How to create a new object instance from a Type?

One may not always know the Type of an object at compile-time, but may need to create an instance of the Type. How do you get a new object instance from a Type?we can achieve this using the Activator class within the root System namespace let us see here How to create a new object instance from a Type?

 

 

 

ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);
ObjectType instance = (ObjectType)Activator.CreateInstance("MyAssembly","MyNamespace.ObjectType");

This above code will help to create a new object instance from a Type.

Leave a Reply

Your email address will not be published. Required fields are marked *