Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activator.CreateInstance with dynamic Type

Tags:

c#

I think I have a lack of understand, what exactly is happening: The user can enter a Path to an Assembly and an Object type and I try to create a instance of it.

My approach:

        Assembly a = Assembly.LoadFile(txtAssemblyPath.Text);
        Type myType = a.GetTypes().ToList().FirstOrDefault(f => f.Name == txtObjectName.Text);

        var obj = Activator.CreateInstance<myType>();
        var obj2 =(myType) Activator.CreateInstance(myType);

The problem is in the Creation of the Object itself. It seems like the myType is not threated as a Type. On this example: Creating generic variables from a type - How? Or use Activator.CreateInstance() with properties { } instead of parameters ( )?

They just get an object, so I guess this is not the same Case. What I dont understand at all: CreateInstance(Type) works, but CreateInstance with the Type doesn't, but T and Type should be the same: System.Type.

Thanks in advance for clarifying.

Matthias

like image 910
Matthias Müller Avatar asked Apr 16 '14 09:04

Matthias Müller


People also ask

What is activator CreateInstance?

The Activator. CreateInstance method creates an instance of a type defined in an assembly by invoking the constructor that best matches the specified arguments. If no arguments are specified then the constructor that takes no parameters, that is, the default constructor, is invoked.

What is activator CreateInstance in net core?

CreateInstance(ActivationContext, String[]) Creates an instance of the type that is designated by the specified ActivationContext object and activated with the specified custom activation data. CreateInstance(Type) Creates an instance of the specified type using that type's parameterless constructor.

What is activator class in C#?

Activator Class in . NET 4.0. Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. ActivatorClassSample.rar. Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects.


2 Answers

There are a using difference... When you write:

var obj = Activator.CreateInstance<myType>();

You used your Class like a Type, and it's the good way to do it. You used a generic type who have a reference to a class type.

But there:

var obj2 =(myType) Activator.CreateInstance(myType);

You used you class like an instance (object). And you can't do that, a class is a schema. If you want to call the second method, you have to write:

var obj2 =(myType) Activator.CreateInstance(typeof(myType));

This code will create an instance of the class Type, and this instance will describe your class myType.

I hope to be clear.

A class is a schema, you can create an object with this schema, it will be an instance (a memory-object of your class).

like image 81
Gaston Siffert Avatar answered Sep 16 '22 22:09

Gaston Siffert


When you use generic type of method such as Activator.CreateInstance<T>(); you have to provide Strong Type of T. That means you have to pass known type name instead of T. for example:

var activatorPerson = (Person)Activator.CreateInstance<Person>();

That's why there is a non generic form of Activator.CreateInstance(typeGoesHere) function which can be used in cases that we do not have a Strong Type at moment of creation of an object. So we can pass type as a parameter to that function. we can provide type variable in lots of ways. In your case you find the proper type in your assembly as following:

Type myType = a.GetTypes().ToList().FirstOrDefault(f => f.Name == txtObjectName.Text);

also you have to notice that explicit casting as you typed in your code is not valid:

obj2 =(myType) Activator.CreateInstance(myType);

because you have to provide Strong Type name for explicit casting. when we do not have access to strong type names at run time, we have to use non generic versions of methods.

like image 35
Abolfazl Hosnoddin Avatar answered Sep 18 '22 22:09

Abolfazl Hosnoddin