简体   繁体   English

不同项目(程序集)之间的反映

[英]Reflection between differents projects (assemblies)

Is there any way, from a given project, to create instances of classes contained in other projects ? 从给定项目开始,有没有办法创建其他项目中包含的类的实例?

I usually use Assembly.GetExecutingAssembly().CreateInstance() , but obviously, this call will return NULL as the class I'm specifying is held in another project. 我通常使用Assembly.GetExecutingAssembly().CreateInstance() ,但显然,这个调用将返回NULL,因为我指定的类保存在另一个项目中。

Any ideas ? 有任何想法吗 ?

If you really want to use reflection to achieve this then look at Assembly.LoadFrom() . 如果你真的想使用反射来实现这一点,那么看看Assembly.LoadFrom() http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx Alternatively, as someone pointed out, you can simply use project references. http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx或者,正如有人指出的那样,您只需使用项目引用即可。

You'll probably want to use Assembly.LoadFrom() to load the assembly from a file if you're trying to do reflection. 如果您正在尝试进行反射,您可能希望使用Assembly.LoadFrom()从文件加载程序集。

But if you simply want to use objects in the assembly, add the assembly as a reference to your project (right click the project in Solution Explorer, and click "Add Reference"). 但是,如果您只想在程序集中使用对象,请添加程序集作为项目的引用(在解决方案资源管理器中右键单击该项目,然后单击“添加引用”)。 Then you can simlpy use all public objects from the referenced assembly. 然后,您可以使用引用的程序集中的所有公共对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM