C# 在设计时不知道类名的情况下创建对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an object without knowing the class name at design time
提问by gil
Using reflection, I need to investigate a user DLL and create an object of a class in it.
使用反射,我需要调查一个用户 DLL 并在其中创建一个类的对象。
What is the simple way of doing it?
这样做的简单方法是什么?
采纳答案by jfs
回答by Nir
You can create an instance of a class from a Type object using Activator.CreateInstance, to get all types in a dll you can use Assembly.GetTypes
您可以使用 Activator.CreateInstance 从 Type 对象创建类的实例,要获取 dll 中的所有类型,您可以使用 Assembly.GetTypes
回答by Mark Ingram
Take a look at these links:
看看这些链接:
http://www.java2s.com/Code/CSharp/Development-Class/Createanobjectusingreflection.htm
http://www.java2s.com/Code/CSharp/Development-Class/Createanobjectusingreflection.htm
http://msdn.microsoft.com/en-us/library/k3a58006.aspx
http://msdn.microsoft.com/en-us/library/k3a58006.aspx
You basically use reflection to load an assembly, then find a type you're interested in. Once you have the type, you can ask to find it's constructors or other methods / properties. Once you have the constructor, you can invoke it. Easy!
您基本上使用反射来加载程序集,然后找到您感兴趣的类型。一旦您拥有该类型,您就可以要求查找它的构造函数或其他方法/属性。一旦有了构造函数,就可以调用它。简单!
回答by samjudson
System.Reflection.Assembly
is the class you will want to use. It contains many method for iterating over the types contained with a user DLL. You can iterate through each class, perhaps see if it inherits from a particular interface etc.
System.Reflection.Assembly
是您要使用的类。它包含许多用于迭代用户 DLL 中包含的类型的方法。您可以遍历每个类,也许看看它是否继承自特定接口等。
http://msdn.microsoft.com/en-us/library/system.reflection.assembly_members.aspx
http://msdn.microsoft.com/en-us/library/system.reflection.assembly_members.aspx
Investigate Assembly.GetTypes()
method for getting the list of types, or Assembly.GetExportedTypes()
for the public ones only.
调查Assembly.GetTypes()
获取类型列表的方法,或Assembly.GetExportedTypes()
仅用于公共列表。
回答by Martin Marconcini
As it has already been said, you need to poke the System.Reflection namespace.
正如已经说过的,您需要戳 System.Reflection 命名空间。
If you know in advance the location/name of the DLL you want to load, you need to iterate through the Assembly.GetTypes().
如果您事先知道要加载的 DLL 的位置/名称,则需要遍历 Assembly.GetTypes()。
In Pseudocode it would look something like this:
在伪代码中,它看起来像这样:
Create and assembly object.
创建和组装对象。
Iterate through all the types contained in the assembly.
遍历程序集中包含的所有类型。
Once you find the one you are looking for, invoke it (CreateInstance)…
一旦你找到你正在寻找的那个,调用它(CreateInstance)......
Use it wisely.
明智地使用它。
;)
;)
I have plenty of Reflection code if you want to take a look around, but the task is really simple and there are at least a dozen of articles with samples out there in the wild. (Aka Google). Despite that, the MSDN is your friend for Reflection Reference.
如果您想环顾四周,我有很多反射代码,但任务非常简单,至少有十多篇带有示例的文章。(又名谷歌)。尽管如此,MSDN 是反射参考的朋友。