C# 在运行时c中编译自定义方法
我想在运行时的项目中编译一个c类或方法。 我可以在运行时编译我的方法,但不幸的是,我不能在运行时编译复杂的方法或从数据库检索数据 我使用此代码,并且它运行良好:C# 在运行时c中编译自定义方法,c#,C#,我想在运行时的项目中编译一个c类或方法。 我可以在运行时编译我的方法,但不幸的是,我不能在运行时编译复杂的方法或从数据库检索数据 我使用此代码,并且它运行良好: string sourceCode = @" public class Calc_Class { public int Rent (int parameter , int a) { int b= 20 ; if(a+parameter > 10 ){ return parameter+3; } return parameter
string sourceCode = @"
public class Calc_Class {
public int Rent (int parameter , int a) {
int b= 20 ;
if(a+parameter > 10 ){
return parameter+3;
}
return parameter += 42;
}
}";
string sourceCode2 = @"
public class Calc_Class {
public int Rent (int parameter , int a) {
int b= 20 ;
if(a+parameter > 10 ){
for(int i=0;i<5;i++){
parameter = parameter+i;
}
retrun parameter;
}
return parameter += 42;
}
}";
var compParms = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
var csProvider = new CSharpCodeProvider();
CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, sourceCode);
object typeInstance = compilerResults.CompiledAssembly.CreateInstance("Calc_Class");
MethodInfo mi = typeInstance.GetType().GetMethod("Rent");
int methodOutput = (int)mi.Invoke(typeInstance, new object[] { 1, 19 });
textBox2.Text = methodOutput.ToString();
如果我调用sourceCode,它会正常工作,但sourceCode2有一个错误
like:无法加载文件或程序集的file://AppData\Local\Temp\v4nza4bk.dll'或其依赖项之一。系统找不到指定的文件
每次运行我的项目后,dll名称都将更改。
如何解决此问题?您可以修复使用编译器参数编译的dll的名称,您可以指定OutputAssembly:
CompilerParameters compParms = new CompilerParameters();
compParms.OutputAssembly = @"d:\NameAssembly.dll";
或者在compParms初始化期间添加该选项。您可以修复使用编译器参数编译的dll的名称,您可以指定OutputAssembly:
CompilerParameters compParms = new CompilerParameters();
compParms.OutputAssembly = @"d:\NameAssembly.dll";
或者在compParms初始化期间添加该选项。您在sourceCode2上有一个输入错误 retrun-->返回 事实上,这条信息并没有帮助。。。 但是如果你修正了输入错误,你的代码就可以正常工作了 关于您的连接字符串错误: 以下方法已粘贴源代码。。。 如果你在你的C中复制粘贴这个,你会发现有很多错误需要修复。 首先修复这些问题,并使其作为一种方法正常运行
void SourceCode3()
{
string connection = Data Source =.;
Initial Catalog = TestReport;
Integrated Security = True;
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand(SELECT * from FormulaOne))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
textBox2.Text = sdr[Formula].ToString();
}
con.Close();
}
}
}
我希望你能达到这样的目标:
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=.; Initial Catalog=TestReport; Integrated Security = True;";
con.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT * from FormulaOne");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
System.Data.SqlClient.SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
textBox2.Text = sdr["Formula"].ToString();
con.Close();
为了使其成为字符串,请记住,您需要在每个字符串前面加一个\
希望这对您有所帮助您在sourceCode2上有输入错误 retrun-->返回 事实上,这条信息并没有帮助。。。 但是如果你修正了输入错误,你的代码就可以正常工作了 关于您的连接字符串错误: 以下方法已粘贴源代码。。。 如果你在你的C中复制粘贴这个,你会发现有很多错误需要修复。 首先修复这些问题,并使其作为一种方法正常运行
void SourceCode3()
{
string connection = Data Source =.;
Initial Catalog = TestReport;
Integrated Security = True;
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand(SELECT * from FormulaOne))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
textBox2.Text = sdr[Formula].ToString();
}
con.Close();
}
}
}
我希望你能达到这样的目标:
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=.; Initial Catalog=TestReport; Integrated Security = True;";
con.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT * from FormulaOne");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
System.Data.SqlClient.SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
textBox2.Text = sdr["Formula"].ToString();
con.Close();
为了使其成为字符串,请记住,您需要在每个字符串前面加一个\
希望这对你有所帮助对我来说,你的例子是按原样工作的。。。我现在看到。。。我不得不把它改成sourceCode2:-p对我来说,你的例子是按原样工作的。。。我现在看到。。。我不得不将其更改为sourceCode2:-p感谢您的帮助,而且我不知道如何使用连接到数据库的方法,它也有您正在编译的此类错误-运行脚本连接到数据库?我想编译一个与连接到数据库的方法相同的方法,检索一些数据,然后我必须对该数据进行一些处理。这是我的代码:string sourceCode3=@string connection=data Source=。;初始目录=测试报告;综合安全=真实;使用SqlConnection con=new SqlConnectionconnection{using SqlCommand cmd=new SqlCommandSELECT*from FormulaOne{cmd.CommandType=CommandType.Text;cmd.Connection=con;con.Open;使用SqlDataReader sdr=cmd.ExecuteReader{sdr.Read;textBox2.Text=sdr[Formula].ToString;}con.Close;};我可以在您提供的连接字符串中看到几个输入错误。看上面的答案。谢谢你的帮助,我也不知道如何使用连接到数据库的方法,它也有这样的错误,你正在编译-运行脚本连接到数据库?我想编译一个与连接到数据库的方法相同的方法,检索一些数据,然后我必须对该数据进行一些处理。这是我的代码:string sourceCode3=@string connection=data Source=。;初始目录=测试报告;综合安全=真实;使用SqlConnection con=new SqlConnectionconnection{using SqlCommand cmd=new SqlCommandSELECT*from FormulaOne{cmd.CommandType=CommandType.Text;cmd.Connection=con;con.Open;使用SqlDataReader sdr=cmd.ExecuteReader{sdr.Read;textBox2.Text=sdr[Formula].ToString;}con.Close;};我可以在您提供的连接字符串中看到几个输入错误。看看上面的答案。