AutoCAD C# 利用反射导出所注册的命令
主函数导出某一程序集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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具