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;
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM