【发布时间】:2016-07-09 02:47:57
【问题描述】:
在下面的代码中,我需要添加我的程序集,以便脚本可以使用它的类:
var options = ScriptOptions.Default.AddImports("MyAssembly");
var code = "using MyAssembly.MyNamespace;" +
"public class TestClass {" +
" public int HelloWorld(int num) {" +
" return 5 + num;" +
" }" +
"}";
但是抛出以下异常:
抛出异常:Microsoft.CodeAnalysis.Scripting.dll 中的“Microsoft.CodeAnalysis.Scripting.CompilationErrorException” Microsoft.CodeAnalysis.Scripting.CompilationErrorException:错误 CS0246:找不到类型或命名空间名称“MyAssembly”(您是否缺少 using 指令或程序集引用?)
我也在宿主项目中添加了程序集。我也尝试了here 中的示例,但它们也不起作用。
添加程序集的正确语法是什么?
【问题讨论】:
-
我之前尝试过
.WithImports(new string[] { "namespace.Whatever" });,但没有成功。现在使用.AddReferences(typeof(namespace.Whatever).Assembly)谢谢! :)
标签: c# roslyn roslyn-code-analysis