首页 > 编程 > .NET > 正文

用反射调用任意.net库中的方法

2024-07-10 12:55:27
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

  函数如下,有注释,请自行查看:)注:1、returnmessage 是我自写的一个类,作用请查看我的其他文章,我也把这个类一并附在后面2、通过namespaceandclassname和methodname实际上就可以精确定位一个方法了如调用abc.dll里的namespace1.class1.main,调用起来就是callassembly("abc.dll","namespace1.class1","main",null) public static returnmessage callassembly(string path,string namespaceandclassname,string methodname,object[] parameters)
        {
            try
            {
                assembly ass=assembly.loadfrom(path);//调入文件(不限于dll,exe亦可,只要是.net)
                type tp=ass.gettype(namespaceandclassname);//namespaceandclassname是"名字空间.类名",如"namespace1.class1"
                methodinfo mi=tp.getmethod(methodname);//methodname是要调用的方法名,如"main"
                object meobj=system.activator.createinstance(tp);
                mi.invoke(meobj,parameters);//parameters是调用目标方法时传入的参数列表
                return new returnmessage(true,"成功调用",1);
            }
            catch(exception e)
            {
                return new returnmessage(false,"出现异常,消息为:"+e.message,-1,e);
            }
        } 附returnmessagepublic class returnmessage
    {
        public returnmessage()
        {
            this.m_succeed=false;
            this.m_message="";
            this.m_code=-1000;
            this.m_data=null;
        }
        public returnmessage(bool issucceed)
        {
            this.m_succeed=issucceed;
        }
        public returnmessage(bool issucceed,string message)
        {
            this.m_succeed=issucceed;
            this.m_message=message;
        }
        public returnmessage(bool issucceed,string message,int code)
        {
            this.m_succeed=issucceed;
            this.m_message=message;
            this.m_code=code;
        }
        public returnmessage(bool issucceed,string message,int code,object data)
        {
            this.m_succeed=issucceed;
            this.m_message=message;
            this.m_code=code;
            this.m_data=data;
        }
        public returnmessage(bool issucceed,string message,int code,object data,object[] datas)
        {
            this.m_succeed=issucceed;
            this.m_message=message;
            this.m_code=code;
            this.m_data=data;
            this.m_datas=datas;
        }
        //
        bool m_succeed;
        string m_message;
        int m_code;
        object m_data;
        object[] m_datas;
        public bool succeed
        {
            get{return m_succeed;}
            set{m_succeed=value;}
        }
        public string message
        {
            get{return m_message;}
            set{m_message=value;}
        }
        public int code
        {
            get{return m_code;}
            set{m_code=value;}
        }
        public object data
        {
            get{return m_data;}
            set{m_data=value;}
        }
        public object[] datas
        {
            get{return m_datas;}
            set{m_datas=value;}
        }
    }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表