I have a string variable that represents the name of a custom class. Example:
我有一个字符串变量,表示自定义类的名称。例:
string s = "Customer";
I will need to create an arraylist of customers. So, the syntax needed is:
我需要创建一个客户的arraylist。所以,所需的语法是:
List<Customer> cust = new ..
How do I convert the string s to be able to create this arraylist on runtime?
如何转换字符串s以便能够在运行时创建此arraylist?
2 个解决方案
#1
58
Well, for one thing ArrayList
isn't generic... did you mean List<Customer>
?
好吧,有一件事,ArrayList不是通用的...你的意思是List
You can use Type.GetType(string)
to get the Type
object associated with a type by its name. If the assembly isn't either mscorlib or the currently executing type, you'll need to include the assembly name. Either way you'll need the namespace too.
您可以使用Type.GetType(string)通过名称获取与类型关联的Type对象。如果程序集不是mscorlib或当前正在执行的类型,则需要包含程序集名称。无论哪种方式,您都需要命名空间。
Are you sure you really need a generic type? Generics mostly provide compile-time type safety, which clearly you won't have much of if you're finding the type at execution time. You may find it useful though...
你确定你真的需要通用类型吗?泛型主要提供编译时类型的安全性,如果你在执行时找到类型,那么显然你不会有太多的东西。你会发现它很有用......
Type elementType = Type.GetType("Customer");
Type listType = typeof(List<>).MakeGenericType(new Type[] { elementType });
object list = Activator.CreateInstance(listType);
If you need to do anything with that list, you may well need to do more generic reflection though... e.g. to call a generic method.
如果你需要对该列表做任何事情,你可能需要做更多的通用反思,例如...调用泛型方法。
#2
12
This is a reflection question. You need to find the type then instantiate an instance of it, something like this:
这是一个反思问题。您需要找到该类型然后实例化它的实例,如下所示:
Type hai = Type.GetType(classString,true);
Object o = (Activator.CreateInstance(hai)); //Or you could cast here if you already knew the type somehow
or, CreateInstance(assemblyName, className)
或者,CreateInstance(assemblyName,className)
You will need to watch out for namespace/type *es though, but that will do the trick for a simple scenario.
您需要注意命名空间/类型冲突,但这样做可以解决一个简单的场景。
Also, wrap that in a try/catch! Activator.CreateInstance is scary!
另外,将它包装在try / catch中! Activator.CreateInstance很吓人!
#1
58
Well, for one thing ArrayList
isn't generic... did you mean List<Customer>
?
好吧,有一件事,ArrayList不是通用的...你的意思是List
You can use Type.GetType(string)
to get the Type
object associated with a type by its name. If the assembly isn't either mscorlib or the currently executing type, you'll need to include the assembly name. Either way you'll need the namespace too.
您可以使用Type.GetType(string)通过名称获取与类型关联的Type对象。如果程序集不是mscorlib或当前正在执行的类型,则需要包含程序集名称。无论哪种方式,您都需要命名空间。
Are you sure you really need a generic type? Generics mostly provide compile-time type safety, which clearly you won't have much of if you're finding the type at execution time. You may find it useful though...
你确定你真的需要通用类型吗?泛型主要提供编译时类型的安全性,如果你在执行时找到类型,那么显然你不会有太多的东西。你会发现它很有用......
Type elementType = Type.GetType("Customer");
Type listType = typeof(List<>).MakeGenericType(new Type[] { elementType });
object list = Activator.CreateInstance(listType);
If you need to do anything with that list, you may well need to do more generic reflection though... e.g. to call a generic method.
如果你需要对该列表做任何事情,你可能需要做更多的通用反思,例如...调用泛型方法。
#2
12
This is a reflection question. You need to find the type then instantiate an instance of it, something like this:
这是一个反思问题。您需要找到该类型然后实例化它的实例,如下所示:
Type hai = Type.GetType(classString,true);
Object o = (Activator.CreateInstance(hai)); //Or you could cast here if you already knew the type somehow
or, CreateInstance(assemblyName, className)
或者,CreateInstance(assemblyName,className)
You will need to watch out for namespace/type *es though, but that will do the trick for a simple scenario.
您需要注意命名空间/类型冲突,但这样做可以解决一个简单的场景。
Also, wrap that in a try/catch! Activator.CreateInstance is scary!
另外,将它包装在try / catch中! Activator.CreateInstance很吓人!