C# 利用发射动态创建泛型类型的对象,泛型类型支持带惨的构造函数

 static class Program
    {
        static void Main(string[] args)
        {
            var aaa = Create<AAA>("aaa");
            aaa.AAAAAA();
            var bbb = Create<BBB>("bbb");
            bbb.BBBBBB();
            Console.ReadKey();

        }
        static T Create<T>(string connString)
        {
            return (T)Activator.CreateInstance(typeof(T), new object[] { connString });
        }
    }
    public class AAA
    {
        private string str;
        public AAA(string input)
        {
            str = input;
        }
        public string AAAAAA()
        {
            return "AAA" + str;
        }
    }
    public class BBB
    {
        private string str;
        public BBB(string input)
        {
            str = input;
        }
        public string BBBBBB()
        {
            return "BBB" + str;
        }
    }

 

posted @ 2016-07-14 11:13  WmW  阅读(2554)  评论(0编辑  收藏  举报