欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AutoCAD C# 利用反射导出所注册的命令

程序员文章站 2022-06-28 19:57:46
主函数导出某一程序集AutoCAD 注册命令/// ///提取所有的命令 /// /// dll的路径 /// public static List GetDllCmds(params string[] dllFiles) { List res = new List(); List cmd... ......

主函数导出某一程序集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;
        }


    }