C#中如何使用类名作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2194949/
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 use class name as parameter in C#
提问by jojo
what i want to do is to automatically create some object.
我想要做的是自动创建一些对象。
In Java, class can be pass as parameter, for example
在Java中,类可以作为参数传递,例如
Class A{
}
Object createObjectBy(class clazz){
       // .. do construction work here
}
when using it, just ---> createObjectBy(A.class)
it is benefit for a lot of things.
它对很多事情都有好处。
so, how can i do similar thing in C#????
那么,我如何在 C# 中做类似的事情????
采纳答案by Aviad P.
Object createObjectBy(Type clazz){
   // .. do construction work here
    Object theObject = Activator.CreateInstance(clazz);
    return theObject;
}
Usage:
用法:
createObjectBy(typeof(A));
Or you could simply use Activator.CreateInstancedirectly :-)
或者你可以直接使用Activator.CreateInstance:-)
回答by hackerhasid
C# doesn't support this. But what are you trying to do?
C# 不支持这个。但是你想做什么?
You probably could use:
你可能可以使用:
createObjectBy(Type type);
or
或者
createObjectBy<T>();
回答by CARLOS LOTH
Use the class Type. You can return an instance of it by call
使用类类型。您可以通过调用返回它的一个实例
obj.GetType();
or without an object instance
或没有对象实例
typeof(className);
I hope it helps.
我希望它有帮助。
回答by Pierre-Alain Vigeant
The ideal way would be to use generics
理想的方法是使用泛型
Type is known at design time
类型在设计时已知
public static T CreateInstance<T>() where T: new()
{
    // Do some business logic
    Logger.LogObjectCreation(typeof(T));
    // Actualy instanciate the object
    return new T();
}
A call example would look like
一个调用示例看起来像
var employee = CreateInstance<Employee>();
Type is unknown at runtime
类型在运行时未知
If the type of object is unknown at runtime, for example through a plugin system, you need to use the Typeclass:
如果对象的类型在运行时未知,例如通过插件系统,则需要使用Type该类:
public static object CreateInstance(Type type)
{
    // Do some business logic
    Logger.LogObjectCreation(type);
    // Actualy instanciate the object
    return Activator.CreateInstance(type);
}
A call example would look like
一个调用示例看起来像
var instance = CreateInstance(someType);
Performance
表现
Of course, nothing beats instanciating an object than by using the keyword new. Except maybe not instanciating, but instead reusing an object, like through caching.
当然,没有什么比使用关键字实例化对象更好的了new。除了可能不是实例化,而是重用一个对象,比如通过缓存。
If you have to settle for the second method where the type is unknown, there are some alternativesto Activator.CreateInstance. Although the article recommend using lambda expression, your biggest consideration is:
如果您不得不接受类型未知的第二种方法,则有一些替代方法Activator.CreateInstance。虽然文章推荐使用 lambda 表达式,但你最大的考虑是:
- Does your unknown object need to be instantiated often in a short period of time, or do you only create it once
- 你的未知对象是否需要在短时间内经常实例化,还是只创建一次
If you only need to create your object once, just stick with the Activator.CreateInstance method. If you need to create it multiple time in a short time, try the lambda approach. That last approach is similar to a compiled regular expression vs. an on-the-fly regular expression.
如果您只需要创建一次对象,请坚持使用 Activator.CreateInstance 方法。如果您需要在短时间内多次创建它,请尝试使用 lambda 方法。最后一种方法类似于编译的正则表达式与动态正则表达式。

