0

I could not find a way of creating a class object from given type and assembly definition using reflection. For my case, I have a string containing class and assembly name and need to create the object:

string str = "My.Assembly.Namesapce.MyClass, My.Assembly.Namesapce, Version=4.0.0.0, Culture=neutral, PublicKeyToken=84474dc3c6524430";
var objClass = --SOME CODE TO USE  STR AND CREATE MyClass--

I have tried to split and create class but that is not very good way of doing it.

Vishnu
  • 2,135
  • 2
  • 30
  • 51
  • The Class you are looking for is called Activator. Have a look at this [answer](http://stackoverflow.com/a/755/4369295) – BoeseB Oct 18 '16 at 09:01
  • 1
    Possible duplicate of [Create an instance of a class from a string](http://stackoverflow.com/questions/223952/create-an-instance-of-a-class-from-a-string) – Venkata Dorisala Oct 18 '16 at 09:02
  • 1
    Random question: is it possible that it isn't working because of the `My.Assembly.Namesapce` typo? (twice) – Marc Gravell Oct 18 '16 at 09:04
  • @MarcGravell No - that will be his next problem ;-) He's definitely looking for the code to actually convert a string to an instance - hence the `--SOME CODE TO USE STR AND CREATE MyClass--` comment. – RB. Oct 18 '16 at 09:09
  • Is the `MyClass` type in the `MyClass objClass` declaration the same `MyClass` as is mentioned in the string? If so then why do you even need reflection? Simply do `MyClass objClass = new MyClass(...);` ? – Lasse V. Karlsen Oct 18 '16 at 09:14
  • I have changed `MyClass` to `var`. The assembly already exists in solution and I'm able to create object of `MyClass` separately but in a scenario the string from database is coming and the object generated may or may not be of type `MyClass` (which I will test in next statement in code) – Vishnu Oct 18 '16 at 09:23

2 Answers2

4
Type type = Type.GetType(str);
object objClass = Activator.CreateInstance(type);

however, to do anything useful with the object you will have to use a common interface / base-class, reflection, or dynamic.

Note: if you can type the variable as MyClass objClass, then the simpler version is of course:

MyClass objClass = new MyClass();
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

You need to do two things:

1) Convert your string str into a type, and 2) Create an instance of that type.

string typeString = "My.Assembly.Namesapce.MyClass, My.Assembly.Namesapce, Version=4.0.0.0, Culture=neutral, PublicKeyToken=84474dc3c6524430";

// Get a reference to the type.
Type theType = Type.GetType(typeString, true, false);

// Create an instance of that type.
MyClass  theInstance = (MyClass)Activator.CreateInstance(theType);
RB.
  • 36,301
  • 12
  • 91
  • 131
  • You can also construct an instance by calling the constructor: `object theInstance = theType.GetConstructor(Type.EmptyTypes).Invoke(null)`, but note that it isn't typical that you write your code with a typed variable like that, if you already have the type, there's no need for the reflection so most likely either `object` or some interface type that is expected to be implemented by the class goes in place of `MyClass` when declaring `theInstance`. – Lasse V. Karlsen Oct 18 '16 at 09:09
  • @Lasse To be honest, I generally use an IoC container to create an instance, which can make managing constructor arguments much easier (as you don't need to know them a-priori, or assume a no-arg constructor). – RB. Oct 18 '16 at 09:10
  • Yes, so do I, just in the context of this question that's probably overkill :) – Lasse V. Karlsen Oct 18 '16 at 09:14