1. 程式人生 > 其它 >反射操作dll類庫之普通類和各種方法呼叫

反射操作dll類庫之普通類和各種方法呼叫

一、使用方法

  1. 查詢DLL檔案,

  2. 通過Reflection反射類庫裡的各種方法來操作dll檔案

二、步驟

  • 載入DLL檔案

      Assembly assembly1 = Assembly.Load("SqlServerDB");//方式一:這個DLL檔案要在啟動專案下
                string filePath = Environment.CurrentDirectory + "";
                Assembly assembly2 = Assembly.LoadFile(filePath + @"\SqlServerDB.dll");//方式二:完整路徑
                Assembly assembly3 = Assembly.LoadFrom(filePath + @"\SqlServerDB.dll");//方式三:完整路徑
                Assembly assembly4 = Assembly.LoadFrom(@"SqlServerDB.dll");//方式三:完整路徑
    
  • 獲取指定型別

    foreach (var item in assembly4.GetTypes())//查詢所有的型別,就是有多少個類
                {
                    Console.WriteLine(item.Name);
                }
    
  • 獲取建構函式

      Type type = assembly4.GetType("SqlServerDB.ReflectionTest");//在ReflectionTest類中呼叫
                foreach (var ctor in type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
                {
                    Console.WriteLine($"構造方法:{ctor.Name}");
                    foreach (var param in ctor.GetParameters())
                    {
                        Console.WriteLine($"構造方法的引數:{param.ParameterType}");
                    }
                }
                //【3】例項化
                //ReflectionTest reflectionTest = new ReflectionTest();//這種例項化是知道具體型別--靜態
    
                //object objTest = Activator.CreateInstance(type);//動態例項化--呼叫我們的構造方法
                object objTest1 = Activator.CreateInstance(type, new object[] { "string" });//動態例項化--呼叫我們的有引數構造方法
    
                //呼叫私有建構函式
                //ReflectionTest reflectionTest = new ReflectionTest();  //普通呼叫
                object objTest2 = Activator.CreateInstance(type, true);
    
  • 呼叫非構造方法

    			  object objTest2 = Activator.CreateInstance(type, true);
    			//呼叫普通方法
                ReflectionTest reflectionTest = objTest2 as ReflectionTest;//as轉換的好處,它不報錯,型別不對的話就返回null
                reflectionTest.Show1();
    
                //呼叫私有方法
                var method = type.GetMethod("Show2", BindingFlags.Instance | BindingFlags.NonPublic);
                method.Invoke(objTest2, new object[] { });
    
  • 呼叫泛型方法

     			//泛型無引數
                var method3 = type.GetMethod("Show3");//查詢指定方法
                var genericMethod = method3.MakeGenericMethod(new Type[] { typeof(int) });//指定泛型引數型別T
                genericMethod.Invoke(objTest2, new object[] { });
    
    
                //泛型有引數
                var method4 = type.GetMethod("Show4");//查詢指定方法
                var genericMethod4 = method4.MakeGenericMethod(new Type[] { typeof(string) });//指定泛型引數型別T
                genericMethod4.Invoke(objTest2, new object[] { 123, "泛型string引數" });
    
  • 反射測試類

    位於SqlServerDB.dll中的ReflectionTest.cs檔案中

     /// <summary>
        /// 反射測試類
        /// </summary>
       public class ReflectionTest
        {
            //私有建構函式
            private ReflectionTest()
            {
                Console.WriteLine("這是私有無引數構造方法");
            }
    
            //普通建構函式
            //public ReflectionTest()
            //{
            //    Console.WriteLine("這是無引數構造方法");
            //}
    
            public ReflectionTest(string name)
            {
                Console.WriteLine($"這是有引數構造方法+引數值是:{name}");
            }
    
            public void Show1()
            {
                Console.WriteLine("呼叫普通方法", this.GetType());
            }
    
            private void Show2()
            {
                Console.WriteLine("呼叫私有方法",this.GetType());
            }
    
    
            public void Show3<T>()
            {
                Console.WriteLine("呼叫無引數泛型方法", this.GetType());
            }
    
            public void Show4<T>(int id,string name)
            {
                Console.WriteLine($"呼叫有引數泛型方法,引數是{id},{name}", this.GetType());
            }
        }