C# and Metadata File Errors

眉间皱痕 提交于 2020-01-24 15:55:29

问题


I've created my own little c# compiler using the tutorial on MSDN, and it's not working properly. I get a few errors, then I fix them, then I get new, different errors, then I fix them, etc etc.

The latest error is really confusing me.

---------------------------

---------------------------
Line number: 0, Error number: CS0006, 'Metadata file 'System.Linq.dll' could not be found;




---------------------------
OK   
---------------------------

I do not know what this means.

Can somebody please explain what's going on here?

Here is my code.

MY SAMPLE C# COMPILER CODE: using System;

namespace JTM
{
    public class CSCompiler
    {
        protected string ot,
            rt,
            ss, es;

        protected bool rg, cg;

        public string Compile(String se, String fe, String[] rdas, String[] fs, Boolean rn)
        {
            System.CodeDom.Compiler.CodeDomProvider CODEPROV = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
            ot =
                fe;

            System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters();
            // Ensure the compiler generates an EXE file, not a DLL.
            PARAMS.GenerateExecutable = true;
            PARAMS.OutputAssembly = ot;

            foreach (String ay in rdas)
            {
                if (ay.Contains(".dll"))
                    PARAMS.ReferencedAssemblies.Add(ay);
                else
                {
                    string refd = ay;
                    refd = refd + ".dll";
                    PARAMS.ReferencedAssemblies.Add(refd);
                }

            }

            System.CodeDom.Compiler.CompilerResults rs = CODEPROV.CompileAssemblyFromFile(PARAMS, fs);

            if (rs.Errors.Count > 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError COMERR in rs.Errors)
                {
                    es = es +
                        "Line number: " + COMERR.Line +
                        ", Error number: " + COMERR.ErrorNumber +
                        ", '" + COMERR.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                // Compilation succeeded.
                es = "Compilation Succeeded.";

                if (rn) System.Diagnostics.Process.Start(ot);
            }
            return es;
        }
    }
}

... And here is the app that passes the code to the above class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] f = { "Form1.cs", "Form1.Designer.cs", "Program.cs" };
            string[] ra = { "System.dll", "System.Windows.Forms.dll", "System.Data.dll", "System.Drawing.dll", "System.Deployment.dll", "System.Xml.dll", "System.Linq.dll" };
            JTS.CSCompiler CSC = new JTS.CSCompiler();
            MessageBox.Show(CSC.Compile(
                textBox1.Text, @"Test Application.exe", ra, f, false));
        }
    }
}

So, as you can see, all the using directives are there. I don't know what this error means. Any help at all is much appreciated.

Thank you


回答1:


Solution:

Add this:

PARAMS.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location);

So the code now looks like this:

namespace JTM
{
    public class CSCompiler
    {
        protected string ot,
            rt,
            ss, es;

        protected bool rg, cg;

        public string Compile(String se, String fe, String[] rdas, String[] fs, Boolean rn)
        {
            System.CodeDom.Compiler.CodeDomProvider CODEPROV = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
            ot =
                fe;

            System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters();
            // Ensure the compiler generates an EXE file, not a DLL.
            PARAMS.GenerateExecutable = true;
            PARAMS.OutputAssembly = ot;
            PARAMS.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location);
            foreach (String ay in rdas)
            {
                if (ay.Contains(".dll"))
                    PARAMS.ReferencedAssemblies.Add(ay);
                else
                {
                    string refd = ay;
                    refd = refd + ".dll";
                    PARAMS.ReferencedAssemblies.Add(refd);
                }

            }

            System.CodeDom.Compiler.CompilerResults rs = CODEPROV.CompileAssemblyFromFile(PARAMS, fs);

            if (rs.Errors.Count > 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError COMERR in rs.Errors)
                {
                    es = es +
                        "Line number: " + COMERR.Line +
                        ", Error number: " + COMERR.ErrorNumber +
                        ", '" + COMERR.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                // Compilation succeeded.
                es = "Compilation Succeeded.";

                if (rn) System.Diagnostics.Process.Start(ot);
            }
            return es;
        }
    }
}


来源:https://stackoverflow.com/questions/2661127/c-sharp-and-metadata-file-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!