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.

6
  • The Class you are looking for is called Activator. Have a look at this answer Commented Oct 18, 2016 at 9:01
  • 1
    Possible duplicate of Create an instance of a class from a string Commented Oct 18, 2016 at 9:02
  • 1
    Random question: is it possible that it isn't working because of the My.Assembly.Namesapce typo? (twice) Commented Oct 18, 2016 at 9: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. Commented Oct 18, 2016 at 9: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(...); ? Commented Oct 18, 2016 at 9:14

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

Comments

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);

3 Comments

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 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).
Yes, so do I, just in the context of this question that's probably overkill :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.