C# 如何在 .NET 3.5 中进行动态对象创建和方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/483215/
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
How to do dynamic object creation and method invocation in .NET 3.5
提问by thr
How does the code looks that would create an object of class:
创建类对象的代码看起来如何:
string myClass = "MyClass";
Of the above type, and then call
上述类型,然后调用
string myMethod = "MyMethod";
On that object?
在那个物体上?
采纳答案by Jon Skeet
- Use
Type.GetType(string)
to get the type object. - Use
Activator.CreateInstance(Type)
to create an instance. - Use
Type.GetMethod(string)
to retrieve a method. - Use
MethodBase.Invoke(object, object[])
to invoke the method on the object
- 使用
Type.GetType(string)
来获取类型的对象。 - 使用
Activator.CreateInstance(Type)
创建一个实例。 - 使用
Type.GetMethod(string)
检索的方法。 - 使用
MethodBase.Invoke(object, object[])
调用该方法的对象
Example, but with no error checking:
示例,但没有错误检查:
using System;
using System.Reflection;
namespace Foo
{
class Test
{
static void Main()
{
Type type = Type.GetType("Foo.MyClass");
object instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("MyMethod");
method.Invoke(instance, null);
}
}
class MyClass
{
public void MyMethod()
{
Console.WriteLine("In MyClass.MyMethod");
}
}
}
Each step needs careful checking - you may not find the type, it may not have a parameterless constructor, you may not find the method, you may invoke it with the wrong argument types.
每一步都需要仔细检查 - 您可能找不到类型,它可能没有无参数构造函数,您可能找不到方法,您可能使用错误的参数类型调用它。
One thing to note: Type.GetType(string) needs the assembly-qualified name of the type unless it's in the currently executing assembly or mscorlib.
需要注意的一件事:Type.GetType(string) 需要类型的程序集限定名称,除非它在当前执行的程序集或 mscorlib 中。
回答by tvanfosson
The following assumes an object with a public constructor and a public method that returns some value but takes no parameters.
下面假设一个对象有一个公共构造函数和一个返回一些值但不带参数的公共方法。
var object = Activator.CreateInstance( "MyClass" );
var result = object.GetType().GetMethod( "MyMethod" ).Invoke( object, null );
回答by Serhat Ozgel
Assuming that your class is in your executing assembly, your constructor and your method is parameterless.
假设你的类在你的执行程序集中,你的构造函数和你的方法是无参数的。
Type clazz = System.Reflection.Assembly.GetExecutingAssembly().GetType("MyClass");
System.Reflection.ConstructorInfo ci = clazz.GetConstructor(new Type[] { });
object instance = ci.Invoke(null); /* Send parameters instead of null here */
System.Reflection.MethodInfo mi = clazz.GetMethod("MyMethod");
mi.Invoke(instance, null); /* Send parameters instead of null here */
回答by Ricardo Amores
I've created a library which simplifies dynamic object creation and invocation using .NET you can download the library and the code in google code: Late Binding HelperIn the project you will find a Wiki page with the usage, or you can also check this article in CodeProject
我创建了一个使用 .NET 简化动态对象创建和调用的库,您可以下载库和谷歌代码中的代码:Late Binding Helper在项目中,您将找到一个包含用法的Wiki 页面,或者您也可以查看这个代码项目中的文章
Using my library, your example will look like this:
使用我的库,您的示例将如下所示:
IOperationInvoker myClass = BindingFactory.CreateObjectBinding("MyClassAssembly", "MyClass");
myClass.Method("MyMethod").Invoke();
Or even shorter:
或者更短:
BindingFactory.CreateObjectBinding("MyClassAssembly", "MyClass")
.Method("MyMethod")
.Invoke();
It uses a fluent interface, and truly simplifies this kind of operations. I hope you could find it useful.
它使用流畅的界面,真正简化了这种操作。我希望你会觉得它很有用。