场景如下
string dataTypeName = "Employee";
其中Employee是一个类,我想为它创建一个对象。
Type EmployeeObject = Type.GetType(dataTypeName);
现在我想实例化Employee的对象,像这样
EmployeeObject emp1 = new Employee();
是可能的吗?但这种方法行不通。我对反射的概念不太清楚。
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);
}
}