博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
遍历文件夹下所有dll的类
阅读量:5252 次
发布时间:2019-06-14

本文共 6118 字,大约阅读时间需要 20 分钟。

(1) 定义所有的类中所有成员的类:

[Serializable]    public class TypeDefine    {        ///         /// 主键Id        ///         public int Id { get; set; }        ///         /// 程序集名称        ///         public string Assembly { get; set; }        ///         /// 命名空间        ///         public string Namespace { get; set; }        ///         /// 类型        ///         public string Type { get; set; }        ///         /// 成员类型(方法,属性,字段)        ///         public string MemberType { get; set; }        ///         /// 成员名称        ///         public string MemberName { get; set; }        ///         /// 参数        ///         public string Parameter { get; set; }        ///         /// 返回值类型        ///         public string ReturnType { get; set; }        ///         /// 结果Hash值        ///         public string Hash { get; set; }        ///         /// 版本号        ///         public int Version { get; set; }        ///         /// 更改类型(1 新增,2 删除)        ///         public int EditType { get; set; }        ///         /// 接口类型        ///         public string InterfaceType { get; set; }        ///         /// 实际版本        ///         public string RealVersion { get; set; }        ///         /// 获取Hash        ///         /// 
public void SetHash() { string key = $"{this.Namespace}-{this.Assembly}-{this.Type}-{this.MemberType}-{this.MemberName}-{this.Parameter}-{this.ReturnType}"; var input = Encoding.UTF8.GetBytes(key); MD5 md5 = MD5.Create(); var result = md5.ComputeHash(input); StringBuilder sb = new StringBuilder(); foreach (byte b in result) { sb.Append(b.ToString("X2")); } this.Hash= sb.ToString(); } }

(2)扫描所有的dll

///         /// 扫描后端        ///         /// bin目录        private List
ScanBack(string filePath) { var path = filePath + "\\bin"; List
excludeList = null; string excludeConfig = ConfigurationManager.AppSettings["excludeDll"]; if (string.IsNullOrEmpty(excludeConfig) == false) { excludeList = excludeConfig.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); } List
allResult = new List
(); foreach (var file in Directory.GetFiles(path, "*.dll")) { string fi = Path.GetFileName(file); //排除不需要扫描的dll if (excludeList != null && excludeList.Contains(fi)) { continue; } var assebmly = Assembly.LoadFrom(file); foreach (var type in assebmly.GetExportedTypes()) { var result = GetExport(fi, type); if (result?.Count > 0) { allResult.AddRange(result); } } } return allResult; }

(3)解析类型的所有的方法、属性等

private List
GetExport(string fileName, Type type) { List
allResult = new List
(); string ns = type.Namespace; string assembly = fileName; string typeName = GetFriendlyName(type); BindingFlags flag = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; foreach (var mi in type.GetMethods(flag)) { var model = new TypeDefine() { Namespace = ns, Assembly = assembly, Type = typeName }; if (mi.DeclaringType == typeof(object)) { continue; } if (mi.Name.StartsWith("get_") || mi.Name.StartsWith("set_") || mi.Name.StartsWith("add_") || mi.Name.StartsWith("remove_")) { continue; ; } model.MemberType = "方法"; model.MemberName = mi.Name; StringBuilder sb = new StringBuilder(); sb.Append("("); bool bDone = true; foreach (var p in mi.GetParameters()) { if (bDone) { bDone = false; sb.AppendFormat("{0} {1}", GetFriendlyName(p.ParameterType), p.Name); } else { sb.AppendFormat(", {0} {1}", GetFriendlyName(p.ParameterType), p.Name); } } sb.Append(")"); model.Parameter = sb.ToString(); model.ReturnType = GetFriendlyName(mi.ReturnType); allResult.Add(model); } foreach (var pi in type.GetProperties(flag)) { var model = new TypeDefine(); model.MemberType = "属性"; model.MemberName = pi.Name; allResult.Add(model); } foreach (var fi in type.GetFields(flag)) { var model = new TypeDefine(); if (fi.Name == "value__") { continue; } model.MemberType = "字段"; model.MemberName = fi.Name; allResult.Add(model); } foreach (var evt in type.GetEvents(flag)) { var model = new TypeDefine(); model.MemberType = "事件"; model.MemberName = evt.Name; allResult.Add(model); } foreach (var obj in allResult) { obj.Namespace = ns; obj.Assembly = assembly; obj.Type = typeName; obj.InterfaceType = "后端"; } return allResult; }

  

 

转载于:https://www.cnblogs.com/caiyongxi/p/7028038.html

你可能感兴趣的文章
Ubuntu下面安装eclipse for c++
查看>>
C#压缩或解压(rar和zip文件)
查看>>
让IE浏览器支持CSS3圆角属性的方法
查看>>
巡风源码阅读与分析---nascan.py
查看>>
LiveBinding应用 dataBind 数据绑定
查看>>
Linux重定向: > 和 &> 区别
查看>>
nginx修改内核参数
查看>>
【欧拉函数模板题】最大公约数
查看>>
C 筛选法找素数
查看>>
TCP为什么需要3次握手与4次挥手(转载)
查看>>
IOC容器
查看>>
织梦仿站第三课:网站的文件分割
查看>>
Windows 2003全面优化
查看>>
URAL 1002 Phone Numbers(KMP+最短路orDP)
查看>>
web_day4_css_宽度
查看>>
用sql删除数据库重复的数据的方法
查看>>
学习笔记21—PS换图片背景
查看>>
electron入门心得
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>
Spring3.0 AOP 具体解释
查看>>