C# 在运行时创建方法和System.IO异常
你好,我一直在努力解决这个问题,但现在无法得到正确的。我发现了一些与我想做的类似的线程。但我一直在找system.Io程序集,无法编译并运行创建的字符串C# 在运行时创建方法和System.IO异常,c#,reflection,C#,Reflection,你好,我一直在努力解决这个问题,但现在无法得到正确的。我发现了一些与我想做的类似的线程。但我一直在找system.Io程序集,无法编译并运行创建的字符串 private bool CalculateBooleanExpression(string expression) { var classExpression = stringClass.Replace(ReplaceMe,expression); var complierParameters
private bool CalculateBooleanExpression(string expression)
{
var classExpression = stringClass.Replace(ReplaceMe,expression);
var complierParameters = new CompilerParameters()
{
GenerateExecutable=false,
GenerateInMemory = true
};
var complier = new CSharpCodeProvider();
var compilerResults = complier.CompileAssemblyFromSource(complierParameters, classExpression);
//break point here compilerResults.CompiledAssembly is null
object typeInstance = compilerResults.CompiledAssembly.CreateInstance("BooleanEvaluator");
MethodInfo methodInfo = typeInstance.GetType().GetMethod("Calculate");
bool value = (bool)methodInfo.Invoke(typeInstance, new object[] { });
return true;
}
无法加载文件或程序集的file:///C:\Users\james.tays\AppData\Local\Temp\22ozhhme.dll'或其依赖项之一。系统找不到指定的文件
有人能告诉我如何调试它,这样我就可以找出哪里有错误了吗
编辑:
调试时我也发现了这个错误
错误CS0116:命名空间不能直接包含字段或方法等成员}
compileasemblyfromsource
返回一个CompilerResults
对象,该对象包含有关尝试编译程序集的结果的大量信息,包括错误和实际输出路径。您是否可以自己进行更多的调试,以查看编译过程中是否有任何错误?我已更新最有可能的是常量或变量stringClass
的值不正确您是否可以包括导致错误的表达式的值?如果出现错误,则代码不可编译。如果要从现有保存的源文件进行编译,请尝试使用compileasemblyfromfile
,而不是使用compileasemblyfromfource
,当字符串包含可编译的源代码而不是文件路径时使用该选项。哪一行抛出错误,编译器生成输出DLL文件?