在运行时编译代码时添加其他引用

时间:2012-09-19 04:53:36

标签: c# asp.net codedom system.diagnostics json.net

我找到了这个程序(http://support.microsoft.com/kb/304655),我在运行时编译代码,它适用于使用引用的代码,

using System;

以下是在运行时编译代码的程序的代码,

        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        ICodeCompiler icc = codeProvider.CreateCompiler();

        string Output = "Out.exe";
        Button ButtonObject = (Button)sender;

        textBox2.Text = "";
        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
        //Make sure we generate an EXE, not a DLL
        parameters.GenerateExecutable = true;
        parameters.OutputAssembly = Output;
        CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);

        if (results.Errors.Count > 0)
        {
            textBox2.ForeColor = Color.Red;
            foreach (CompilerError CompErr in results.Errors)
            {
                textBox2.Text = textBox2.Text +
                            "Line number " + CompErr.Line +
                            ", Error Number: " + CompErr.ErrorNumber +
                            ", '" + CompErr.ErrorText + ";" +
                            Environment.NewLine + Environment.NewLine;
            }
        }
        else
        {
            //Successful Compile
            textBox2.ForeColor = Color.Blue;
            textBox2.Text = "Success!";
            //If we clicked run then launch our EXE
            if (ButtonObject.Text == "Run") Process.Start(Output);
        }

以下是我需要在运行时编译的代码,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Text.RegularExpressions;
    using Newtonsoft.Json.Linq;

    namespace Tsubame
    {
        class Program
        {
            static void Main(string[] args)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"url");

                // Create Client
                WebClient client = new WebClient();

                // Assign Credentials
                client.Credentials = new NetworkCredential("user", "pass");

                //Grab Data
                var data = client.DownloadString(@"url");

                JObject o = JObject.Parse(data);
                string getFristRow = Convert.ToString(o["Body"][0]["RowId"]);

                string encaplulateStart = "\\\"";
                string encaplulateEnd = "\\\":";

                List<string> _matches = new List<string>();
                _matches = Regex.Matches(getFristRow, @"(?<=" + encaplulateStart + ").*(?=" + encaplulateEnd + ")")
                                    .Cast<Match>()
                                    .Select(m => m.Value)
                                    .ToList();

                foreach (string head in _matches)
                {
                    Console.WriteLine(head);

                }
                Console.ReadLine();
            }
        }
    }

但是当我输入时会给出错误代码

    Error Number: CS0234

对于System以外的参考。我可以知道如何在运行时添加额外的引用,以便它可以成功编译:)非常感谢:))

1 个答案:

答案 0 :(得分:5)

您需要使用CompilerParameters.ReferencedAssemblies在<{1}}中添加引用:

CompilerParameters

(当然你不必使用对象初始化器语法来设置它,但它使它更整洁IMO。)