c# 反射调用方法
文件目录:
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[] { "龙卷风摧毁停车场" });
运行结果: