c# 反射呼叫方法

龙卷风吹毁停车场發表於2024-10-08
檔案目錄:

SqlServerDBHlper.cs檔案內容:

using System.Xml.Linq;

namespace ZHAOXI.DBHlper
{
    public class SqlServerDBHlper
    {
        public SqlServerDBHlper()
        {
            Console.WriteLine(this.GetType() + "=====無引數建構函式");
        }
        public SqlServerDBHlper(string name)
        {
            Console.WriteLine(this.GetType() + "=====有引數建構函式: "+ name);
        }
        public void Test()
        {
            Console.WriteLine(this.GetType() + "=====無引數過載方法");
        }
        public void Test(string name)
        {
            Console.WriteLine(this.GetType() + "=====有引數過載方法string: " + name);
        }
        public void Test(string name, int id)
        {
            Console.WriteLine(this.GetType() + "=====有引數過載方法string,int: " + name+" " +id);
        }
        public void Test(int id, string name)
        {
            Console.WriteLine(this.GetType() + "=====有引數過載方法int,string: " + id + " " + name);
        }

        private void Show()
        {
            Console.WriteLine(this.GetType() + "===Show==無引數私有方法");
        }
        private void Show(string name)
        {
            Console.WriteLine(this.GetType() + "===Show==有引數私有方法name: " + name);
        }
        private void Show(string name,int id)
        {
            Console.WriteLine(this.GetType() + "===Show==有引數私有方法name,id: " + name+" "+id);
        }

        public static void Prod()
        {
            Console.WriteLine(typeof(SqlServerDBHlper) + "===無引數靜態方法");
        }
        public static void Prod(string name)
        {
            Console.WriteLine(typeof(SqlServerDBHlper) + "===有引數靜態方法name: "+ name);
        }
    }
}

Program.cs內容:

using System.Reflection;

//Assembly assembly = Assembly.LoadFrom("ZHAOXI.DBHlper.dll");
//Assembly assembly = Assembly.Load("ZHAOXI.DBHlper");
Assembly assembly = Assembly.LoadFile(@"C:\Users\YCFK-228\Desktop\asp.net core\ZAOXI.JIAOYU\bin\Debug\net8.0\ZHAOXI.DBHlper.dll");
Type type = assembly.GetType("ZHAOXI.DBHlper.SqlServerDBHlper");
object? obj = Activator.CreateInstance(type);

//呼叫有引數構造方法
Activator.CreateInstance(type, new object[] { "龍捲風摧毀停車場" });

//呼叫無引數過載方法
MethodInfo methodInfo = type.GetMethod("Test",new Type[] {});
methodInfo.Invoke(obj, null);

//呼叫有引數過載方法
MethodInfo methodInfo1 = type.GetMethod("Test", new Type[] {typeof(string)});
methodInfo1.Invoke(obj, new object[] {"龍捲風摧毀停車場"});

//呼叫有引數過載方法
MethodInfo methodInfo2 = type.GetMethod("Test", new Type[] { typeof(string),typeof(int) });
methodInfo2.Invoke(obj, new object[] { "龍捲風摧毀停車場", 10086 });

//呼叫有引數過載方法
MethodInfo methodInfo3 = type.GetMethod("Test", new Type[] { typeof(int), typeof(string) });
methodInfo3.Invoke(obj, new object[] { 10086, "龍捲風摧毀停車場" });

//呼叫無引數私有方法
MethodInfo methodShow = type.GetMethod("Show", BindingFlags.Instance | BindingFlags.NonPublic,new Type[] {});
methodShow.Invoke(obj, null);

//呼叫有引數私有方法
MethodInfo methodShow1 = type.GetMethod("Show", BindingFlags.Instance | BindingFlags.NonPublic, new Type[] { typeof(string)});
methodShow1.Invoke(obj, new object[] { "龍捲風摧毀停車場" });

//呼叫有引數私有方法
MethodInfo methodShow3 = type.GetMethod("Show", BindingFlags.Instance | BindingFlags.NonPublic, new Type[] { typeof(string) ,typeof(int)});
methodShow3.Invoke(obj, new object[] { "龍捲風摧毀停車場" ,10086});

//呼叫無引數靜態方法
MethodInfo methodShow4 = type.GetMethod("Prod",new Type[] {});
methodShow4.Invoke(obj, new object[] { });
//呼叫無引數靜態方法
MethodInfo methodShow5 = type.GetMethod("Prod", new Type[] { typeof(string)});
methodShow5.Invoke(obj, new object[] { "龍捲風摧毀停車場" });

執行結果:

相關文章