反射_案例2_根据参数类型获取构造器<15/9/2017>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace 案例2
{
class Program
{
static void Main(string[] args)
{
Type t = Type.GetType("案例2.Person");
Type[] ts = new Type[] {Type.GetType("System.Int32"),Type.GetType("System.String") };
//另一种常用方法为:Type[] ts = new Type[] {typeof(int),typeof(string)};
t.GetConstructor(ts);
ConstructorInfo ci = t.GetConstructor(ts);
ci.Invoke(new object[] { 1, "abc" });
}
}
public class Person
{
public Person()
{
Console.WriteLine("11111111111");
}
public Person(int id, string name)
{
Console.WriteLine("22222222222");
}
public Person(string name, int id)
{
Console.WriteLine("33333333333");
}
}
}