包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用。此类不能被继承——c# Activator
c#在类工厂中动态创建类的实例
1. Activator.CreateInstance (Type)
2. Activator.CreateInstance (Type, Object[])
public class ActivatorTest
{
private string name;
public string Name { set { name = value; }get { return name; } }
private string age;
public string Age { set { age = value; }get { return age; } }
private string mon;
public string Mon { set { mon = value; }get { return mon; } }
public ActivatorTest() { }
public void ActivatorTestInfo(string name,string age,string mon)
{
this.name = name;
this.age = age;
this.mon = mon;
Console.WriteLine("Here Here Here");
}
}
public static void Main()
{
ActivatorTest activatorTest = new ActivatorTest(); ;
Type type = null;
MethodInfo methodInfo = null;
Object resClass = null;
Object[] paramters = { "1","2","3"};
type = activatorTest.GetType();
methodInfo = type.GetMethod(type.Name+"Info");//搜索公共方法
resClass = Activator.CreateInstance(type);//动态创建类的实例
methodInfo.Invoke(resClass, paramters);//执行类,参数
}