C# 在运行时编译代码时添加其他引用
我发现这个程序()在运行时编译代码,它适用于使用引用的代码C# 在运行时编译代码时添加其他引用,c#,asp.net,codedom,system.diagnostics,json.net,C#,Asp.net,Codedom,System.diagnostics,Json.net,我发现这个程序()在运行时编译代码,它适用于使用引用的代码 using System; 以下是在运行时编译代码的程序的代码 CSharpCodeProvider codeProvider = new CSharpCodeProvider(); ICodeCompiler icc = codeProvider.CreateCompiler(); string Output = "Out.exe"; Button ButtonObj
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();
}
}
}
用于系统以外的参考。请告诉我如何在运行时添加其他引用,以便它能够成功编译:)非常感谢:)您需要使用以下方法在
编译器参数中添加引用:
(当然,您不必使用对象初始值设定项语法来设置它,但在我看来,它使它更整洁。)非常感谢!!就是这样:)我使用了lyk参数.referencedAssemblys.Add(“Newtonsoft.Json.dll”);这和你的方法有区别吗??thanx:)而var参数存根在代码上尝试时会给出错误,有什么原因吗??请原谅,如果这是一个简单的解决方案,但我是新的这方面的编程:)
Error Number: CS0234
var parameters = CompilerParameters
{
GenerateExecutable = true,
OutputAssembly = Output,
ReferencedAssemblies = {
"System.dll",
"System.Core.dll",
// etc
}
};