编译器无法看到System.ComponentModel库 [英] The compiler can not see the System.ComponentModel library

查看:88
本文介绍了编译器无法看到System.ComponentModel库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来动态创建一个类



 StringBuilder sourcecode =  StringBuilder(); 
sourcecode.Append( using System; + Environment.NewLine);
sourcecode.Append( using System.ComponentModel; + Environment.NewLine);
sourcecode.Append( namespace testThis + Environment.NewLine + {);
sourcecode.Append( public class Sample {}});

CompilationResult = RuntimeCompiler.CompileAssemblyFromSource(参数,sourcecode.ToString());





但它给了我编译错误CS0234:名称空间''System'中不存在类型或命名空间名称''ComponentModel''(你是否缺少程序集引用?)}



要获取我写的DLL文件名

System.ComponentModel.SomeCLassName,我把光标放在这个类名上并按下F12,我得到了文件

  string  s = @   C:\\ \\ Program Program \ Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll 

并尝试使用
$加载它b $ b

 Assymbly a = Assymbly.LoadFrom(s)



在构造字符串之前,但它仍然给我同样的错误。

我需要一些帮助来解决这个问题。

解决方案

右键单击项目并选择添加引用。在Framework列表中查找System.ComponentModel并添加它或适用的子程序集。这应该是您需要做的全部。



忘记尝试使用Assembly.LoadFrom添加引用。完全是错误的方法。


你做错了;这是根本的事情。没有它,您就无法开发任何.NET产品。所以,你没有发展。 :-)



首先,程序集引用。使用 Assembly.LoadFrom 是高级主题Reflection。我们不在这里讨论它。

首先,您需要了解汇编和GAC的概念:

http://en.wikipedia.org/wiki/Assembly_%28programming%29 [ ^ ],

http://msdn.microsoft.com/en-us/library/hk5f40ct%28v=vs.90%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/ library / s1sx4kfb.aspx [ ^ ],

http://en.wikipedia.org/wiki/Global_Assembly_Cache [ <一个href =http://en.wikipedia.org/wiki/Global_Assembly_Cachetarget =_ blanktitle =New Window> ^ ]。



全局程序集缓存(GAC)中的程序集使用其强名称引用(甚至动态加载),而不是DLL位置。您永远不应该依赖于确切的位置:平台不保证它。您正在尝试使用硬编码的路径名。在任何情况下,硬编码的路径名称都不会有用,即使对于数据文件也是如此。



另请参阅:

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/x4cw969y.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ky3942xh.aspx [ ^ ]。



在Visual Studio中,要引用程序集,您应该使用添加引用窗口的.NET选项卡。



-SA

好的,感谢我的评论答案。我刚刚测试了它,问题出在 CompilerParameters 传递给 CompileAssemblyFromSource ,你一定错过了参考到System.dll



例如

  String  [] referenceAssemblies = {  System.dll}; 
CompilerParameters cp = new CompilerParameters(referenceAssemblies);
cp.GenerateInMemory = true ;
cp.GenerateExecutable = false ;
cp.OutputAssembly = My.Dynamic.dll;
CompilerResults cr = RuntimeCompiler.CompileAssemblyFromSource(cp,source);





如果没有该引用,我会得到同样的错误错误CS0234:类型或命名空间名称ComponentModel在命名空间System中不存在(你是否缺少程序集引用?)当我执行代码时。



Alan。


I wrote the following code to create a class dynamically

StringBuilder sourcecode = new StringBuilder();
sourcecode.Append("using System;" + Environment.NewLine);
sourcecode.Append("using System.ComponentModel;" + Environment.NewLine);
sourcecode.Append("namespace testThis " + Environment.NewLine +"{");
sourcecode.Append("public class Sample { } }"); 

CompilationResult = RuntimeCompiler.CompileAssemblyFromSource(Parameters, sourcecode.ToString());



But it gives me Compiler error CS0234: The type or namespace name ''ComponentModel'' does not exist in the namespace ''System'' (are you missing an assembly reference?)}

To get the DLL file name I wrote
System.ComponentModel.SomeCLassName, I put the cursor on this classname and pressed F12, I got the file

string s = @ "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll"

and tried to load it using

Assymbly a = Assymbly.LoadFrom(s)


before constructing the string, but it still gives me the same error.
I need some help to resolve this problem.

解决方案

Right-click on your Project and select "Add Reference". Find System.ComponentModel in the Framework list and add it or the applicable sub assembly. That should be all you need to do.

Forget trying to add a reference with the Assembly.LoadFrom stuff. Totally the wrong way to do that.


You are doing it wrong; and this is the fundamental thing. Without it, you cannot develop any .NET products. So, you are not developing. :-)

First of all, assemblies are referenced. Using Assembly.LoadFrom is the advanced topic, Reflection. Let''s not discuss it here.
First, you need to understand the concept of assembly and GAC:
http://en.wikipedia.org/wiki/Assembly_%28programming%29[^],
http://msdn.microsoft.com/en-us/library/hk5f40ct%28v=vs.90%29.aspx[^],
http://msdn.microsoft.com/en-us/library/s1sx4kfb.aspx[^],
http://en.wikipedia.org/wiki/Global_Assembly_Cache[^].

Assemblies in Global Assembly Cache (GAC) are referenced (and even loaded dynamically) using their strong names, not DLL location. You should never rely on exact location: the platform does not guarantee it. You are trying to use a hard-coded path name. There are no situations where hard-coded path names can be useful, never at all, even for data files.

See also:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/x4cw969y.aspx[^],
http://msdn.microsoft.com/en-us/library/ky3942xh.aspx[^].

In Visual Studio, to reference an assembly, you should use the tab ".NET" of the "Add Reference" window.

—SA


OK, thanks for the answer to my comment. I''ve just tested it and the problem is in the CompilerParameters passed to CompileAssemblyFromSource and you must have missed the reference to System.dll

e.g.

String[] referenceAssemblies = { "System.dll" };
CompilerParameters cp = new CompilerParameters(referenceAssemblies);
cp.GenerateInMemory = true;
cp.GenerateExecutable = false;
cp.OutputAssembly = "My.Dynamic.dll";
CompilerResults cr = RuntimeCompiler.CompileAssemblyFromSource(cp, source);



Without that reference I get that same error "error CS0234: The type or namespace name ''ComponentModel'' does not exist in the namespace ''System'' (are you missing an assembly reference?)" when I execute the code.

Alan.


这篇关于编译器无法看到System.ComponentModel库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆