create instance of class programmatically

  • Thread starter Thread starter Bjoern
  • Start date Start date
B

Bjoern

I have a name of a class and the corresponding assembly.
Now I want to create an object of this class.
I think it is should work with Reflection. But I don't
know how exactly.

Could someone post a little sample in C# code.
Thanks in advance,
Bjoern
 
Bjoern,

Assuming you have the full name of the assembly, you can do this:

// Load the assembly.
// You might need a different overload or method on Assembly to load the
assembly.
Assembly assembly = Assembly.Load(assemblyName);

// Get the type from the assembly.
Type type = assembly.GetType(typeName);

// Create an instance. You might need to call an overload that takes
parameters.
assembly.CreateInstance(type);

Hope this helps.
 
Back
Top