注:以下代码仅供参考使用,实际使用过程,应根据实际场景--进行提取重构,提升复用效率......
简单理解,反射应用于加载动态库DLL,解析其内部实现.......
示例如下所示:注,该代码基于.NET 5
运行效果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | using System; using System.Reflection; using TestDLL; namespace Reflection { /// <summary> /// ---------------------C# 反射应用------------------ /// 加载、调用(TestDLL.dll为例--应和启动程序同目录)、实例化... /// </summary> class Program { static void Main( string [] args) { Console.WriteLine( "-----------------加载 DLL------------" ); Assembly assembly = LoadDLL( @"TestDLL.dll" ); //加载 DLL //Type type = assembly.GetType("TestDLL.ReflectionTest"); Type type = GetTypeByNamespace(assembly, "TestDLL.ReflectionTest" ); Console.WriteLine( "-----------------获取指定动态库的所有实例------------" ); GetTypes(assembly); Console.WriteLine( "-----------------构造调用------------" ); object obj = GetStructMethod(type, "反射反射来一个" ); //GetStructAllMethod(type);//构造调用 Console.WriteLine( "-----------------普通方法调用------------" ); ReflectionTest show = obj as ReflectionTest; show.Show1(); //temp.Show3<ReflectionTest>(); //temp.Show4<ReflectionTest>("Show4入参测试"); Console.WriteLine( "-----------------私有方法调用------------" ); GetPrivateMethod(type, obj, "Show2" ); Console.WriteLine( "-----------------泛型方法调用------------" ); //泛型方法调用 GetMethodByName(type, obj, "Show3" ); GetMethodByNameAndParams(type, obj, "Show4" , "有参泛型方法调用测试入参" ); Console.WriteLine( "-----------------泛型类 + 泛型方法------------" ); //泛型类 + 泛型方法(一定要给定具体的类型参数) `1表示一个参数 GetGenericMethod(assembly, "TestDLL.GenericClass`1" , "GenericMethod" ); //设置属性和获取属性 Console.WriteLine( "-----------------设置属性和获取属性------------" ); GetProperty(assembly, "TestDLL.PropertyClass" ); Console.ReadLine(); } private static void GetProperty(Assembly assembly, string propertyClass) { Type type = assembly.GetType(propertyClass); object obj = Activator.CreateInstance(type); int index = 0; foreach ( var item in type.GetProperties()) { Console.WriteLine( "属性名称==" + item.Name); index++; //测试一把 if (item.Name.Equals( "Id" )) item.SetValue(obj, index); if (item.Name.Equals( "Name" )) item.SetValue(obj, "Name" + index); if (item.Name.Equals( "NickName" )) item.SetValue(obj, "NickName" + index); } } private static void GetGenericMethod(Assembly assembly, string genericClass, string methodName) { Type type = assembly.GetType(genericClass).MakeGenericType( typeof ( string )); object obj = Activator.CreateInstance(type); var method = type.GetMethod(methodName).MakeGenericMethod( new Type[] { typeof ( string ) }); method.Invoke(obj, new object [] { }); } /// <summary> /// 泛型方法调用--无参 /// </summary> /// <param name="type">Type type</param> /// <param name="obj">object obj</param> /// <param name="methodName">方法名称</param> private static void GetMethodByNameAndParams(Type type, object obj, string methodName, string paramStr) { //查找指定的方法 var method = type.GetMethod(methodName); //指定泛型参数类型 var temp = method.MakeGenericMethod( new Type[] { typeof ( string ) }); temp.Invoke(obj, new object [] { paramStr }); } /// <summary> /// 泛型方法调用--无参 /// </summary> /// <param name="type">Type type</param> /// <param name="obj">object obj</param> /// <param name="methodName">方法名称</param> private static void GetMethodByName(Type type, object obj, string methodName) { //查找指定的方法 var method = type.GetMethod(methodName); //指定泛型参数类型 var temp = method.MakeGenericMethod( new Type[] { typeof ( string ) }); temp.Invoke(obj, new object [] { }); } /// <summary> /// 泛型方法调用 /// </summary> /// <param name="type">Type type</param> /// <param name="obj">object obj</param> /// <param name="methodName">方法名称</param> private static void GetPrivateMethod(Type type, object obj, string methodName) { var method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(obj, new object [] { }); } /// <summary> /// 获取指定动态库的所有实例 /// </summary> /// <param name="assembly">Assembly assembly</param> /// <returns></returns> private static Type[] GetTypes(Assembly assembly) { var temps = assembly.GetTypes(); foreach ( var item in temps) { Console.WriteLine( "name==" + item.Name); } return temps; } /// <summary> /// 构造调--实例化 /// </summary> /// <param name="type"></param> private static void GetStructAllMethod(Type type) { 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); } } } /// <summary> /// 构造调--实例化 /// </summary> /// <param name="type"></param> private static object GetStructMethod(Type type, string paramStr) { //动态获取实例类型--var/object类型(无参构造调用) //var temp = Activator.CreateInstance(type); //动态获取实例类型--var/object类型(有参构造调用) var temp = Activator.CreateInstance(type, new object [] { paramStr }); //私有构造方法调用() var privateStruct = Activator.CreateInstance(type, true ); return privateStruct; } /// <summary> /// 通过反射获取指定类型(TestDLL.dll为例) /// </summary> /// <param name="assembly">Assembly assembly</param> /// <param name="namespaceAll">Assembly assembly</param> /// <returns></returns> private static Type GetTypeByNamespace(Assembly assembly, string namespaceAll) { return assembly.GetType(namespaceAll); } /// <summary> /// 加载 DLL /// </summary> /// <returns></returns> private static Assembly LoadDLL( string dllName) { //建议使用方式 Assembly assembly = Assembly.LoadFrom(dllName); //@"TestDLL.dll" //加载DLL文件 加载文件需在项目启动位置 //Assembly assembly1 = Assembly.Load("TestDLL"); //不建议使用方式 //Assembly assembly3 = Assembly // .LoadFrom(@"D:\常用dll整理包\C# 反射应用\Reflection\bin\Debug\net5.0\TestDLL.dll"); //Assembly assembly4 = Assembly // .LoadFile(@"D:\常用dll整理包\C# 反射应用\Reflection\bin\Debug\net5.0\TestDLL.dll"); return assembly; } } } using System; namespace TestDLL { public class GenericClass<T> { public void GenericMethod<TType>() { Console.WriteLine( "GenericMethod方法调用" ); } } } namespace TestDLL { public class PropertyClass { public int Id { get ; set ; } public string Name { get ; set ; } public string NickName { get ; set ; } } } using System; namespace TestDLL { public class ReflectionTest { public ReflectionTest() { Console.WriteLine( "私有调用无参构造方法成功" ); } //public ReflectionTest() //{ // Console.WriteLine("调用无参构造方法成功"); //} public ReflectionTest( string paramValue) { Console.WriteLine( "调用有参构造方法成功,参数=" + paramValue); } public void Show1() { Console.WriteLine( "Show1普通方法调用" , this .GetType()); } private void Show2() { Console.WriteLine( "Show2私有方法调用" , this .GetType()); } public void Show3<T>() { Console.WriteLine( "Show3泛型方法调用" , this .GetType()); } public void Show4<T>( string name) { Console.WriteLine( "Show4泛型方法调用,参数是==" + name, this .GetType()); } } } using System; namespace TestDLL { public class ReflectionTest2 { public ReflectionTest2() { Console.WriteLine( "ReflectionTest2调用无参构造方法成功" ); } public ReflectionTest2( string paramValue) { Console.WriteLine( "ReflectionTest2调用有参构造方法成功,参数=" + paramValue); } } } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-01-03 angularJs select