熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用反射調用任意.net庫中的方法

2022-06-13   來源: .NET編程 

注:
ReturnMessage 是我自寫的一個類作用請查看我的其他文章我也把這個類一並附在後面
通過NameSpaceAndClassName和MethodName實際上就可以精確定位一個方法了如調用abcdll裡的namespaceClassMain調用起來就是CallAssembly(abcdllnamespaceClassMainnull)
 
public static ReturnMessage CallAssembly(string Pathstring NameSpaceAndClassNamestring MethodNameobject[] Parameters)
        {
            try
            {
                Assembly Ass=AssemblyLoadFrom(Path);//調入文件(不限於dllexe亦可只要)
                Type TP=AssGetType(NameSpaceAndClassName);//NameSpaceAndClassName是名字空間類名namespaceClass
                MethodInfo MI=TPGetMethod(MethodName);//MethodName是要調用的方法名Main
                object MeObj=SystemActivatorCreateInstance(TP);
                MIInvoke(MeObjParameters);//Parameters是調用目標方法時傳入的參數列表
                return new ReturnMessage(true成功調用);
            }
            catch(Exception e)
            {
                return new ReturnMessage(false出現異常消息為:+eMessagee);
            }
        }
 
附ReturnMessage
public class ReturnMessage
    {
        public ReturnMessage()
        {
            thism_Succeed=false;
            thism_Message=;
            thism_Code=;
            thism_Data=null;
        }
        public ReturnMessage(bool IsSucceed)
        {
            thism_Succeed=IsSucceed;
        }
        public ReturnMessage(bool IsSucceedstring Message)
        {
            thism_Succeed=IsSucceed;
            thism_Message=Message;
        }
        public ReturnMessage(bool IsSucceedstring Messageint Code)
        {
            thism_Succeed=IsSucceed;
            thism_Message=Message;
            thism_Code=Code;
        }
        public ReturnMessage(bool IsSucceedstring Messageint Codeobject Data)
        {
            thism_Succeed=IsSucceed;
            thism_Message=Message;
            thism_Code=Code;
            thism_Data=Data;
        }
        public ReturnMessage(bool IsSucceedstring Messageint Codeobject Dataobject[] Datas)
        {
            thism_Succeed=IsSucceed;
            thism_Message=Message;
            thism_Code=Code;
            thism_Data=Data;
            thism_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;}
        }
    }

From:http://tw.wingwit.com/Article/program/net/201311/12297.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.