C#反射

來源:互聯網
上載者:User

標籤:++   obj   else   span   director   cte   pac   lis   c#   

    protected void Page_Load(object sender, EventArgs e)      {          Invoke("Test", "1.0.0.0", "neutral", "8d7b357e8456bb71", "Test", "Class1", "GetMessage",new object[] {"1","p"});      }        private string Invoke(string lpFileName, string Version, string Culture, string PublicKeyToken, string Namespace, string ClassName, string lpProcName,object[] objArr)      {          string ssc = "";          try          { // 載入程式集              Assembly MyAssembly = Assembly.Load(lpFileName + ", Version=" + Version + ", Culture=" + Culture + ", PublicKeyToken=" + PublicKeyToken);              Type[] type = MyAssembly.GetTypes();              foreach (Type t in type)              {// 尋找要調用的命名空間及類                  if (t.Namespace == Namespace && t.Name == ClassName)                  {// 尋找要調用的方法並進行調用                      MethodInfo m = t.GetMethod(lpProcName);                      if (m != null)                      {                          object o = Activator.CreateInstance(t, objArr);                            ssc = (string)m.Invoke(o, null);                          m.Invoke(o, null);                      }                      else                           ssc=" 裝載出錯 !";                  }              }          }//try          catch (System.NullReferenceException e)          {              ssc=e.Message;          }//catch          return ssc;      }  

       /// <summary>        /// 動態讀取DLL,執行其中的方法        /// </summary>        public void LoadAssembly()        {            //DLL所在的絕對路徑             Assembly assembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "Entity.dll");            //注意寫法:程式集.類名              Type type = assembly.GetType("Entity.GetData");            //擷取類中的公用方法GetResule                                                          MethodInfo methed = type.GetMethod("GetResule");             //建立對象的執行個體            object instance = System.Activator.CreateInstance(type);            //執行方法  new object[]為方法中的參數            object result = methed.Invoke(instance, new object[] { });        } 

 

 

 

擷取所有載入的程式集

      System.AppDomain _Domain = System.AppDomain.CurrentDomain;            Assembly[] _AssemblyList = _Domain.GetAssemblies();            IList<string> _DllPath = new List<string>();            for (int i = 0; i != _AssemblyList.Length; i++)            {                _DllPath.Add(_AssemblyList[i].Location);            }

System.Reflection.Assembly.GetEntryAssembly().GetReferencedAssemblies();

 

C#反射

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.