Tomas
Tomas

Reputation: 18097

Instantiate class from class type

What would be correct way to create new instance of the class from class type? The code below do not work, CreateInstance accepts only strings.

Type converter=null;    

converter = convertApiModelBase.Type;

return Assembly.GetExecutingAssembly().CreateInstance(converter);

Upvotes: 0

Views: 122

Answers (2)

amotzg
amotzg

Reputation: 1202

As explained in http://msdn.microsoft.com/en-us/library/aa329906%28v=vs.71%29.aspx

With the code you attached, use the The Type.FullName of the type to locate.

return Assembly.GetExecutingAssembly().CreateInstance(converter.FullName);

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

Use Activator.CreateInstance:

return Activator.CreateInstance(convertApiModelBase.Type);

Upvotes: 2

Related Questions