C# 和元数据文件错误

标签 c# .net winforms compiler-construction codedom

我使用 MSDN 上的教程创建了我自己的小型 c# 编译器,但它无法正常工作。我遇到了一些错误,然后我修复了它们,然后我遇到了新的、不同的错误,然后我修复了它们,等等。

最新的错误真的让我很困惑。

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

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




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

我不知道这是什么意思。

有人可以解释一下这是怎么回事吗?

这是我的代码。

我的示例 C# 编译器代码: 使用系统;

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;
        }
    }
}

...这是将代码传递给上述类的应用程序:

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));
        }
    }
}

因此,如您所见,所有 using 指令都在那里。我不知道这个错误是什么意思。非常感谢任何帮助。

谢谢

最佳答案

解决方法:

添加这个:

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

所以代码现在看起来像这样:

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;
        }
    }
}

关于C# 和元数据文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2661127/

相关文章:

c# - c# 中的时间跨度交集

c# - 这个 Null 异常怎么可能。需要帮助解释调用堆栈

asp.net - 注册 .NET 4.5 IIS 10 Windows 10

winforms - winform中的activex

c# - 更改 Windows 窗体中所有工具提示的工具提示超时

c# - CollectionView 实时排序回调

c# - EmguCV/OpenCV 中的 body 识别(跟踪视频中的人物)

c# - 如何在 C# 中读取 ID3 标签并将其写入 MP3?

c# - 如何计算在我的窗口窗体中按下了多少个按钮?

c# - 将 NULL 插入 SQL Server 数据库