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;
        }


    }

猜你喜欢

转载自www.cnblogs.com/NanShengBlogs/p/10957636.html
今日推荐