2

我找到了这个程序(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

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

4

1 回答 1

6

您需要在CompilerParametersusing中添加引用CompilerParameters.ReferencedAssemblies

var parameters = CompilerParameters
{
    GenerateExecutable = true,
    OutputAssembly = Output,
    ReferencedAssemblies = {
        "System.dll",
        "System.Core.dll",
        // etc
    }
};

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

于 2012-09-19T05:52:19.280 回答