1

Here is scenario

string dataTypeName = "Employee";

where Employee is class and i want to make an object of it.

Type EmployeeObject = Type.GetType(dataTypeName);

Now i want to instantiate an object of Employee like this

EmployeeObject emp1 = new Employee();

Is it Possible. But this method is not working. I don't have strong idea of Reflection. Please Guide me.

Zeb-ur-Rehman
  • 1,121
  • 2
  • 21
  • 37
  • What is the value of - `typeof(Employee).AssemblyQualifiedName`? What is the need to get `EmployeeObject` from the name, and then instansiate it with a literal type anyway - why not just use the literal type in the first place? – LukeHennerley May 28 '13 at 11:01
  • @LukeHennerley Value: Reflection.Employee, Reflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null – Zeb-ur-Rehman May 28 '13 at 11:04
  • http://stackoverflow.com/questions/752/get-a-new-object-instance-from-a-type – Freelancer May 28 '13 at 11:06

1 Answers1

5

CreateObjectfromassemblynameandclassname

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;


public class Reflection
{

    public static T CreateObject<T>(string assemblyName, string className)
    {
        Assembly assembly = Assembly.Load(assemblyName);
        return (T)assembly.CreateInstance(className);
    }
}
Rahul
  • 5,603
  • 6
  • 34
  • 57
Nahum
  • 6,959
  • 12
  • 48
  • 69