C#如何動態調用DLL中類的方法以及屬性

來源:互聯網
上載者:User
在C#中可以通過Assembly來動態載入DLL,然後由它建立類型,接著通過類型的InvokeMember方法來調用DLL中類的方法以及屬性。



為了方便說明一下的方法,先說明一下DLL的代碼,大致如下:

using System;

namespace clsTestDll

{

     /**//// <summary>

     /// Summary description for TestDll.

     /// </summary>

     public class TestDll

     {

         private string strName;

         public TestDll()

         {

             //

             // TODO: Add constructor logic here

             //

             strName = "";

         }



         public string GetValue( int nCount )

         {

             return string.Format( "Count is {0}!", nCount );

         }



         public static string GetNewValue( int nCount )

         {

             return string.Format( "New count is {0}!", nCount );

         }



         public string Name

         {

             get{ return strName;}

             set{ strName = value;}

         }

     }

}



大致的步驟如下:

首先載入DLL,具體如下:

     // Load assembly from dll file

     Assembly assembly = Assembly.LoadFrom( "clsTestDll.dll");   



其次,用載入的assembly來定義指定的類型,例如:

     // Create new type

     Type t = assembly.GetType( "clsTestDll.TestDll");



       然後就可以通過建立的類型來調用類的方法。

       如果是類的靜態方法,可以直接調用,如:

     // Call static member function by name

     string strReturn = (string) t.InvokeMember("GetNewValue", 

         BindingFlags.DeclaredOnly | 

         BindingFlags.Public |

         BindingFlags.Static | BindingFlags.InvokeMethod, 

         null, 

         null, 

         new object[]{ 12 } );



       如果是類的非靜態方法或屬性,則需要通過類型,先產生類的對象,如:

     // Create new object of specific class name

     Object obj = t.InvokeMember(

         null, 

         BindingFlags.DeclaredOnly | 

         BindingFlags.Public | BindingFlags.NonPublic | 

         BindingFlags.Instance | BindingFlags.CreateInstance, 

         null, 

         null,

         null );



       接著,就可以通過“obj”對象來進行調用了,如:

     // Call member function by name

     strReturn = (string) t.InvokeMember("GetValue", 

         BindingFlags.DeclaredOnly | 

         BindingFlags.Public | BindingFlags.NonPublic | 

         BindingFlags.Instance | BindingFlags.InvokeMethod, 

         null, 

         obj, 

         new object[]{ 12 } );



     // Set class property

     t.InvokeMember("Name", 

         BindingFlags.DeclaredOnly | 

         BindingFlags.Public | BindingFlags.NonPublic | 

         BindingFlags.Instance | BindingFlags.SetProperty, 

         null, 

         obj, 

         new Object[] {"Test"} );



     // Get class property

     strReturn = (string) t.InvokeMember("Name", 

         BindingFlags.DeclaredOnly | 

         BindingFlags.Public | BindingFlags.NonPublic | 

         BindingFlags.Instance | BindingFlags.GetProperty, 

         null, 

         obj,

         null);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.