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.

3
  • 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? Commented May 28, 2013 at 11:01
  • @LukeHennerley Value: Reflection.Employee, Reflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Commented May 28, 2013 at 11:04
  • stackoverflow.com/questions/752/… Commented May 28, 2013 at 11:06

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.