c# – 如何使用Reflection获取命名空间,类,方法及其参数
发布时间:2020-12-15 23:29:55 所属栏目:百科 来源:网络整理
导读:我需要使用反射加载dll,并且必须获取该dll的命名空间,类,方法及其参数.此外,我需要在日志文件中写入这些信息. 我使用了以下代码.但是我只得到了写入日志文件的类. Assembly dll = Assembly.LoadFile(@"D:AssembliesMyapplication.dll");foreach (Type type
我需要使用反射加载dll,并且必须获取该dll的命名空间,类,方法及其参数.此外,我需要在日志文件中写入这些信息.
我使用了以下代码.但是我只得到了写入日志文件的类. Assembly dll = Assembly.LoadFile(@"D:AssembliesMyapplication.dll"); foreach (Type type in dll.GetTypes()) { var name = "Myapplication~" + type.FullName; File.AppendAllText("Assembly Details.log","Myapplication~" + type.FullName + "rnrn"); Console.WriteLine(type.FullName); } 在日志文件中,信息需要如下所示. Myapplication~namespace.class~method(arguments) 例如:
任何建议都会有很大帮助. 解决方法
对于公共方法
MethodInfo[] myArrayMethodInfo = type.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly); 对于非公开方法 MethodInfo[] myArrayMethodInfo1 = type.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly); 然后使用提取信息 MethodInfo[] myArrayMethodInfo // Display information for all methods. for(int i=0;i<myArrayMethodInfo.Length;i++) { MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i]; Console.WriteLine("nThe name of the method is {0}.",myMethodInfo.Name); ParameterInfo[] pars = myMethodInfo.GetParameters(); foreach (ParameterInfo p in pars) { Console.WriteLine(p.ParameterType); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Sqlite 常用函数封装:修改密码,创建数据库、获取表段、字
- ruby-on-rails – 测试功能是否足够?
- swift – 如何在UICollectionView的页脚中添加UIActivityIn
- ruby-on-rails – 如何使用dragonfly调整before_save的图像
- c# – BackgroundWorker RunWorkerCompletedEventArgs.Canc
- UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现
- 通过XML设置屏幕方向(android:screenOrientation)详解
- PostgreSQL DBLink:没有函数匹配给定的名称和参数类型
- 用经验诠释Access、Trunk和Hybird端口数据收发规则
- vue-自定义组件传值的实例讲解