无法将 system.dll 引用添加到动态编译的 C#.NET 代码
我正在尝试使用 CSharpCodeProvider 接口动态编译代码,以在运行时动态编译代码,以便基于此 http://www.codeproject.com/KB/cs/codecompilation.aspx文章,它在VS2010中编译没有问题(只有关于ICodeCompiler的警告已弃用,但我尝试了两种方法:忽略警告并保留代码原样,并根据 MSDN 推荐更改代码 - 两种方法都不起作用),但在运行时,我收到动态代码的单个编译器错误,告诉我我忘记添加对 System.dll 的引用,并且根据下面的代码,我不明白我是如何忘记这样做的:
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.dll");
//cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("App.exe");
有趣的是,如果我重复添加 system.dll 的行(注释行),我会收到错误告诉我 System.dll 是双重的参考。有什么想法吗?另外值得一提的是,codeproject 中的示例可以工作,而该代码只是该示例的副本,没有任何更改,则不能。我发现该示例使用 .NET 2.0 和 VC# 2005 编译器,而 VS2010 默认使用 .NET 4.0,因此也尝试更改它,但没有效果。欢迎任何帮助。
提前致谢..
I'm trying to dynamically compile code using CSharpCodeProvider interface to dynamically compile code at runtime for purposes of scripting my application based on this http://www.codeproject.com/KB/cs/codecompilation.aspx article, it compiles with no problems in VS2010(only warnings about ICodeCompiler being deprecated, but I have tried both ways: ignoring the warning and leaving the code as it is, and changing code according to MSDN recommendation - neither way works), but at runtime I get a single compiler error for the dynamic code telling me that I forgot to add reference to System.dll and based on this code below I don't see how did I forget to do so:
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.dll");
//cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("App.exe");
Interestingly enough, if I repeat the line for adding the system.dll( the commented line) I get an error telling me that System.dll is double referenced. Any ideas what this is all about? Also its worth mentioning that the sample from codeproject works, while this code which is just a copy from that sample with no changes at all does not. I figured out that the sample is using .NET 2.0 along side with VC# 2005 Compiler while VS2010 uses .NET 4.0 by default so tried changing that as well but with no avail.Any help is welcome.
Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除两行
cp.ReferencedAssemblies.Add("mscorlib.dll");
remove both lines saying
cp.ReferencedAssemblies.Add("mscorlib.dll");