0

I have the following qualified class name stored in a string:

string className="Project.LogicLayer.FunctionsLayer.Medic";

Now I need a new instance of the class from using that string.

I tried this but it always returns null:

Type t=Type.GetType(className);
object = Activator.CreateInstance(t);
6
  • how about System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(string className) ? Commented Mar 30, 2016 at 21:53
  • 2
    Create an instance of a class from a string Commented Mar 30, 2016 at 21:55
  • Is the class in the same assembly as you are calling Activator.CreateInstance? and is variable t null or not null? Commented Mar 30, 2016 at 21:57
  • I used it, but it returns null too. Commented Mar 30, 2016 at 21:57
  • @JSON, probably you confused with typeof. Commented Mar 30, 2016 at 22:04

1 Answer 1

4

Try this overload of Activator.CreateInstance

Activator.CreateInstance("MedicAssembly", "Project.LogicLayer.FunctionsLayer.Medic");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, now it works, I have based in your example. Assembly asm = Assembly.LoadFrom("C:\\Projec\\AppMedical\\packages\\BussinessLayer\\lib\\net451\\Buessinesslayer.dll"); Type t = asm.GetType(Project.LogicLayer.FunctionsLayer.Medic); var obj = Activator.CreateInstance(t);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.