How to create a new object instance from a Type

Here I am trying to explain How to create a new object instance from a Type. We may not always know the Type of an object at compile-time, but unfortunately we may need to create an instance of the Type, how we will do it , let us see How do you get a new object instance from a Type?

The Activator class in the System namespace is pretty powerful and helpful .

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

Leave a Reply

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