Assembly assembly = Assembly.Load("Ant.DB.SQLServer");//加载方式一:dll文件名(当前目录) Assembly assembly1 = Assembly.LoadFile(@"E:\MyReflection\MyReflection\Ant.DB.SQLServer\bin\Debug\Ant.DB.Interface.dll");//加载方式二:完整路径(文件具体路径) Assembly assembly2 = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//方法三:完全限定名(当前目录) Assembly assembly3 = Assembly.LoadFrom(@"E:\MyReflection\MyReflection\Ant.DB.SQLServer\bin\Debug\Ant.DB.Interface.dll");//(文件具体路径) Console.WriteLine(assembly); Console.WriteLine(assembly1); Console.WriteLine(assembly2); Console.WriteLine(assembly3); foreach (var type in assembly.GetTypes())//找到所有类型 { Console.WriteLine(type.Name); foreach (var item in type.GetMethods()) { Console.WriteLine("method.name= "+ item); } }
Assembly assembly = Assembly.Load("Ant.DB.SQLServer");//加载方式一:dll文件名(当前目录) Type SQLServer_type = assembly.GetType("Ant.DB.SQLServer.SQLServerHelper"); object obj = Activator.CreateInstance(SQLServer_type); SQLServerHelper ServerHelper= obj as SQLServerHelper; Students students = ServerHelper.Find(1); Console.WriteLine(students); Console.WriteLine("----------------------------------------Common--------------------------------");
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll"); Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest"); ConstructorInfo[] constructorInfos = type.GetConstructors(); foreach (var constructor in constructorInfos) { Console.WriteLine(constructor);//显示类型 } Console.WriteLine("************************创建对象************************"); object oCotr1 = Activator.CreateInstance(type);//无参数构造函数 object oCotr2 = Activator.CreateInstance(type,new object[] { "111"}); object oCotr3 = Activator.CreateInstance(type, new object[] { 123}); object oCotr4 = Activator.CreateInstance(type, new object[] { 123,"123" }); #region Common Console.WriteLine("----------------------------------------Common--------------------------------");
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//加载DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.PrivateCtor");//获取到类型 object oPrivate = Activator.CreateInstance(type, true);
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.GenericClass`3");//获取到类型名称 Type makeType = type.MakeGenericType(new Type[] { typeof(int), typeof(string), typeof(double) }); object oGeneric = Activator.CreateInstance(makeType);
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 object oReflection = Activator.CreateInstance(type); foreach (var method in type.GetMethods()) { Console.WriteLine(method.Name); foreach (var parameter in method.GetParameters()) { Console.WriteLine(parameter.Name + " " + parameter.ParameterType); } MethodInfo methodInfo = type.GetMethod("Test1"); methodInfo.Invoke(oReflection, null);//调用方法 }
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 object oReflection = Activator.CreateInstance(type); foreach (var method in type.GetMethods()) { Console.WriteLine(method.Name); foreach (var parameter in method.GetParameters()) { Console.WriteLine(parameter.Name + " " + parameter.ParameterType); } MethodInfo methodInfo = type.GetMethod("Test2"); methodInfo.Invoke(oReflection, new object[] {123456 });//调用方法,携带参数 }
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 object oReflection = Activator.CreateInstance(type); foreach (var method in type.GetMethods()) { Console.WriteLine(method.Name); foreach (var parameter in method.GetParameters()) { Console.WriteLine(parameter.Name + " " + parameter.ParameterType); } MethodInfo methodInfo = type.GetMethod("Test3", new Type[] { typeof(int), typeof(string) }); methodInfo.Invoke(oReflection, new object[] { 133, "123" });//调用方法 }
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 object oReflection = Activator.CreateInstance(type); foreach (var method in type.GetMethods()) { Console.WriteLine(method.Name); foreach (var parameter in method.GetParameters()) { Console.WriteLine(parameter.Name + " " + parameter.ParameterType); } MethodInfo methodInfo = type.GetMethod("Test3", new Type[] { }); methodInfo.Invoke(oReflection, null);//调用方法 }
MethodInfo methodInfo = type.GetMethod("Test5"); methodInfo.Invoke(oReflection, new object[] { "123" });//调用方法
MethodInfo methodInfo = type.GetMethod("Test5"); methodInfo.Invoke(null, new object[] { "123" });//调用方法
Invoke第一个参数为对象的实例对象或null都可。
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 object oReflection = Activator.CreateInstance(type); MethodInfo methodInfo = type.GetMethod("Test4", BindingFlags.Instance | BindingFlags.NonPublic); methodInfo.Invoke(oReflection, new object[] { "123" });
MethodInfo methodGeneric = methodInfo.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof(DateTime) });//确定方法的参数类型和个数 methodGeneric.Invoke(oReflection, new object[] { 1, "Ant编程", DateTime.Now });
MethodInfo methodInfoNew = methodInfo.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof(DateTime) }); methodInfoNew.Invoke(oReflection, new object[] { 1, "Ant编程", DateTime.Now });
Assembly assembly = Assembly.LoadFrom("Ant.DB.SQLServer.dll");//获取DLL文件 Type type = assembly.GetType("Ant.DB.SQLServer.ReflectionTest");//获取到类型名称 object oReflection = Activator.CreateInstance(type); Student studen = new Student() { Id = 1, StudentAddress = "xx", StudentName = "giao" }; foreach (var prop in type.GetProperties()) { Console.WriteLine($"{prop.PropertyType}+{prop.Name}={prop.GetValue(studen)}"); Console.WriteLine("-------------------------------------------------"); if (prop.Name.Equals("Id")) { prop.SetValue(studen, 2); } if (prop.Name.Equals("StudentName")) { prop.SetValue(studen, "giao"); } if (prop.Name.Equals("StudentAddress")) { prop.SetValue(studen, "1111"); } Console.WriteLine($"{prop.PropertyType}+{prop.Name}={prop.GetValue(studen)}"); }
还有个比较简单的方式
PropertyInfo[] propertyInfos = type.GetProperties();//查找所有的属性 PropertyInfo propertyInfo = type.GetProperty("Id");