I have a string variable that represents the name of the custom class. Example:
string s = "customer";
I'll need to create an array list of customers. Therefore, syntax is required:
list & lt; Customers & gt; Cust = new ..
How can I convert the string to be able to create this array list on runtime?
Good, one thing array list
is not normal ... is your Meaning list & lt; Customers & gt;
?
You can type . Type. Gettype (string)
to get a type related type
object by its name. If the assembly is not an MSCirl or current execution type, then you must include the name of the assembly. Either way you will also need a namespace.
Do you really need a normal type? Generics mostly provide compile-time type protection, which obviously will not be too much to you if you are looking for type on execution time. You can be useful Though it seems ...
type elementType = Type.GetType ("customer"); Type listType = typeof (List & lt; & gt;) MakeGenericType (new type [] {elementType}); Object list = activator Createinstance (list type);
If you want to do anything with that list, you may need to do more general reflection ... for example a common method call to do.
Comments
Post a Comment