AutoCAD C# 利用反射导出所注册的命令
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了AutoCAD C# 利用反射导出所注册的命令,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3201字,纯文字阅读大概需要5分钟。
内容图文
![AutoCAD C# 利用反射导出所注册的命令](/upload/InfoBanner/zyjiaocheng/814/ae029cf089ac46e395be3a250b2d3088.jpg)
主函数导出某一程序集AutoCAD 注册命令
/// <summary> ///提取所有的命令 /// </summary> /// <param name="dllFiles">dll的路径</param> /// <returns></returns> public static List<gcadDllcmd> GetDllCmds(params string[] dllFiles) { List<gcadDllcmd> res = new List<gcadDllcmd>(); List<gcadCmds> cmds = new List<gcadCmds>(); #region 提取所以的命令 for (int i = 0; i < dllFiles.Length; i++) { Assembly ass = Assembly.LoadFile(dllFiles[i]);//反射加载dll程序集 var clsCollection = ass.GetTypes().Where(t => t.IsClass && t.IsPublic).ToList(); if (clsCollection.Count > 0) { foreach (var cls in clsCollection) { var methods = cls.GetMethods().Where(m => m.IsPublic && m.GetCustomAttributes(true).Length > 0).ToList(); if (methods.Count > 0) { foreach (MethodInfo mi in methods) { var atts = mi.GetCustomAttributes(true).Where(c => c is CommandMethodAttribute).ToList(); if (atts.Count == 1) { gcadCmds cmd = new gcadCmds(cls.Name, mi.Name, (atts[0] as CommandMethodAttribute).GlobalName, ass.ManifestModule.Name.Substring(0, ass.ManifestModule.Name.Length - 4)); cmds.Add(cmd); } } } } } } #endregion if (cmds.Count > 0) { List<string> dllName = new List<string>(); foreach (var item in cmds) { if (!dllName.Contains(item.dllName)) dllName.Add(item.dllName); } foreach (var item in dllName) res.Add(new gcadDllcmd(item, cmds)); } return res; // }
1# 定义自定义的class,
/// <summary> /// 储存自定义的cad命令的信息的类 /// </summary> public class gcadCmds { public string clsName { get; set; } public string cmdName { get; set; } public string cmdMacro { get; set; } public string dllName { get; set; } public gcadCmds(string _clsName, string _cmdName, string _macro, string _dllName) { this.dllName = _dllName; this.clsName = _clsName; this.cmdMacro = _macro; this.cmdName = _cmdName; } }
2# 定义类
/// <summary> /// 储存包含自定命令的类 /// </summary> public class gcadClscmd { public string clsName { get; set; } public string dllName { get; set; } public bool HasGcadcmds { get; set; } public List<gcadCmds> curClscmds { get; set; } public gcadClscmd(string _clsName, List<gcadCmds> cmds) { this.clsName = _clsName; this.dllName = cmds.First().dllName; var clsCmds = cmds.Where(c => c.clsName == this.clsName).ToList(); if (clsCmds.Count > 0) { this.HasGcadcmds = true; this.curClscmds = new List<gcadCmds>(); foreach (var item in clsCmds) { if (item.clsName == this.clsName) this.curClscmds.Add(item); } } else this.HasGcadcmds = false; } }
3# 存储每个dll的
/// <summary> /// 储存每个dll类的 /// </summary> public class gcadDllcmd { public string DllName { get; set; } public bool HasGcadcls { get; set; } public List<gcadClscmd> clsCmds { get; set; } public List<gcadCmds> curDllcmds { get; set; } public gcadDllcmd(string _dllname, List<gcadCmds> cmds) { this.DllName = _dllname; var curDllcmds = cmds.Where(c => c.dllName == this.DllName).ToList(); if (curDllcmds.Count > 0) { this.HasGcadcls = true; this.curDllcmds = curDllcmds; List<string> listClsName = new List<string>(); foreach (gcadCmds item in this.curDllcmds) { if (!listClsName.Contains(item.clsName)) listClsName.Add(item.clsName); } this.clsCmds = new List<gcadClscmd>(); foreach (var item in listClsName) { gcadClscmd clsCmds = new gcadClscmd(item, this.curDllcmds.Where(c => c.clsName == item).ToList()); this.clsCmds.Add(clsCmds); } } else this.HasGcadcls = false; } }
内容总结
以上是互联网集市为您收集整理的AutoCAD C# 利用反射导出所注册的命令全部内容,希望文章能够帮你解决AutoCAD C# 利用反射导出所注册的命令所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。