加载Dll三种方式介绍
Assembly configExtendsAssembly1 = Assembly.LoadFrom("ConfigExtends.dll");
Assembly configExtendsAssembly2 = Assembly.Load(@"ConfigExtends, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
Assembly configExtendsAssembly3 = Assembly.LoadFile(@"C:\Users\Administrator\source\repos\Study.Reflect\ConfigHandle\bin\Debug\netcoreapp3.1\ConfigExtends.dll");
加载对应的DLL
Assembly configExtendsAssembly1 = Assembly.LoadFrom("ConfigExtends.dll");
Assembly printReflectionTestAssembly2 = Assembly.LoadFrom("PrintReflectionTest.dll");
反射调用方法
var type = configExtendsAssembly1.GetType("ConfigExtends.ConfigService");
var oResult = Activator.CreateInstance(type);
var configServicePrint = type.GetMethod("Print", BindingFlags.Public | BindingFlags.Static);
var print = configServicePrint.Invoke(oResult, new[] { "这是打印方法." });
Console.WriteLine(print.ToString());
var configServiceGetConfig = type.GetMethod("GetConfig", BindingFlags.Public | BindingFlags.Static);
var connectionStrings = configServiceGetConfig.Invoke(oResult, new[] { "ConnectionStrings:defulat" });
Console.WriteLine(connectionStrings?.ToString());
获取反射类型里各种方法(公开的,私有的,公开静态的,内部的)
var type = printReflectionTestAssembly2.GetType("PrintReflectionTest.PrintTest");
var oPrintTest = Activator.CreateInstance(type, true);
var printFunc = type.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.Public);
printFunc.Invoke(oPrintTest, new object[] { "张三" });
var myTestPrint1 = type.GetMethod("MyTestPrint1");
myTestPrint1.Invoke(oPrintTest, null);
var myTestPrint2 = type.GetMethod("MyTestPrint2", new Type[] { typeof(string) });
myTestPrint2.Invoke(oPrintTest, new object[] { "普通方法一个参数" });
var _myTestPrint2 = type.GetMethod("MyTestPrint2", new Type[] { typeof(string), typeof(DateTime) });
_myTestPrint2.Invoke(oPrintTest, new object[] { "普通方法一个参数", DateTime.Now });
var myTestPrint3 = type.GetMethod("MyTestPrint3", BindingFlags.NonPublic | BindingFlags.Instance);
myTestPrint3.Invoke(oPrintTest, new object[] { });
var myTestPrint4 = type.GetMethod("MyTestPrint4", BindingFlags.NonPublic | BindingFlags.Instance);
myTestPrint4.Invoke(oPrintTest, new object[] { });
var getResult = type.GetMethod("GetResult");
var strfunc = getResult.MakeGenericMethod(new Type[] { typeof(string) });
strfunc.Invoke(oPrintTest, new object[] { "给泛型方法指定类型(string)" });
调用反射内泛型方法
var _type = printReflectionTestAssembly2.GetType("PrintReflectionTest.GenericClass`3");
_type = _type.MakeGenericType(new Type[] { typeof(string), typeof(string), typeof(int) });
var oGenericClass = Activator.CreateInstance(_type);
var func = _type.GetMethod("Show");
func.Invoke(oGenericClass, new object[] { "张三", "男", 19 });
_type = printReflectionTestAssembly2.GetType("PrintReflectionTest.GenericMethod");
func = _type.GetMethod("Show");
func = func.MakeGenericMethod(new Type[] { typeof(string), typeof(string), typeof(int) });
var oGenericMethod = Activator.CreateInstance(_type);
func.Invoke(oGenericMethod, new object[] { "张三", "男", 19 });
_type = printReflectionTestAssembly2.GetType("PrintReflectionTest.GenericDouble`1");
_type = _type.MakeGenericType(new Type[] { typeof(string) });
var oGenericDouble = Activator.CreateInstance(_type);
func = _type.GetMethod("Show").MakeGenericMethod(new Type[] { typeof(string), typeof(int) });
func.Invoke(oGenericDouble, new object[] { "张三", "男", 19 });