Search This Blog

Tuesday, January 10, 2012

C# Using reflection to create an instance of a specific type; convert string to a specific type;

The idea behind the post is to convert a string such as: “System.Int32” to an instance of a specific type.
To create an instance of a compiled custom class in the assembly:

Assembly.GetExecutingAssembly().CreateInstance(YourCustomClassName);

To create an instance of a built-in type e.g. “System.Int32” :
Activator.CreateInstance(Type.GetType("System.Int32"));

No comments:

Post a Comment

If you like this post, please leave a comment :)