反射_案例2_根据参数类型获取构造器

来源:互联网 发布:忠邦的梦 知乎 编辑:程序博客网 时间:2024/05/22 17:53

反射_案例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");        }    }}


阅读全文
0 0
原创粉丝点击