What is the C# way for dynamically getting the type of object and then creating new instances of it?
E.g. how do I accomplish the result of following Java code, but in C#:
MyClass x = (MyClass) Class.forName("classes.MyChildClass").newInstance();
What is the C# way for dynamically getting the type of object and then creating new instances of it?
E.g. how do I accomplish the result of following Java code, but in C#:
MyClass x = (MyClass) Class.forName("classes.MyChildClass").newInstance();
Simple example:
Call
Activator.CreateInstance()
specifying an assembly name, and a classname. The classname must be fully qualified (include all the namespaces). If the assemblyname is null, then it uses the currently running assembly. If the class you want to activate is outside the currently running assembly you need to specify the assembly with the fully qualified name.To load from a different assembly, you need to use an overload for
CreateInstance
that accepts a type, as opposed to a type name; one way to get a type for a given type name is to useType.GetType()
, specifying the assembly-qualified name.Most often
Activator.CreateInstance()
is done using interface types, so that the instance created can be cast to the interface and you can invoke on the interface type. like this:and
Doing it that way, your app (the activating app) need not be aware of or reference the other assembly explicitly, at compile time.
Be warned about the formulation of this question. The question is "Class.forName() equivalent in .NET?" and this question is not actually answered on this page - the Class.forName() method in Java has one important side effect that the .NET equivalent does not have - that is it actually loads the class. So if you had in Java
You need TWO statements in C#/.NET to get the full effect:
This loads the class without invoking any static methods or creating any instances.
This question has two parts
How to get the
Type
corresponding to a name?Sometimes you can use
Type.GetType(string)
. But unless the type is in mscorlib or the executing assembly you need to specify the name of the assembly in your name.How to create a given Type?
Activatior.CreateInstance
is the answer to this part.But in your code you already know the class because you can cast to
MyClass
. So the question doesn't make much sense.Look at csharp-examples.net/reflection-examples. Basically you have to use
typeof()
andActivator.createInstance()
.