建立反射執行個體|反射過濾|反射搜尋

來源:互聯網
上載者:User

//建立反射執行個體
方法一:這個方法需要一個字元竄類型的參數,該參數表示要載入的程式集的名稱

Assembly myAssembly = Assembly.Load("System.Drawing");

方法二:這個方法需要傳遞一個包含.NET程式集路徑和名字的字元竄類型的參數

string path = @"C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Drawing.dll";
Assembly myAssembly = Assembly.LoadFrom(path);

方法三:這個方法使用關鍵字typeof擷取程式集

Assembly otherAssembly = typeof(System.Data.DataRow).Assembly;

方法四:GetType方法。該方法可以用在所有.NET對象上

DataTable dt = new DataTable();
Assembly otherAssembly = dt.GetType().Assembly;

 

如有以下控制台程式集
using System;
using System.Reflection;
namespace Basics
{
     public enum testEnum
     {
         testValue = 1
     }

    public class testClass
     {

        public static void Main()
        {

        }

     }
}

為了能夠訪問這兩個類型,你需要調用Assembly.GetTypes 代碼如下

Assembly myAssembly = Assembly.Load("Basics");
Type[] types = myAssembly.GetTypes();
foreach(Type type in types)
{
    if(type.IsClass)
        Console.WriteLine(type.Name);
}

下面的代碼在其自己的基礎上建立了一個Assembly執行個體並且在其包含的類型中迭代

using System;
using System.Reflection;
namespace Basics
{
    public class testClass
    {
        public static void Main()
        {
            Assembly myAssembly = Assembly.Load("Basics");
            Type[] types = myAssembly.GetTypes();
            foreach(Type type in types)//迭代
            {
                Console.WriteLine("Type:{0}",type.Name);
            }
            Console.ReadLine();
        }
    }
}

反射的過濾

如有以下類

public class OtherClass
{
        public void OtherMethod()
        {}
        public static void OtherStaticMethod()
        {}
        public static void AnotherStaticMethod()
        {}
}

在這裡,你會看到,它有三個公用的方法,其中的兩個為靜態方法。假設你想通過反射找出類OtherClass裡的所有公用的靜態方法,你將會調用GetMethods方法,代碼如下

MethodInfo[] mis = typeof(OtherClass).GetMethods(BindingFlags.Public | BindingFlags.Static);

 

 

 

反射的搜尋(Type類的方法FindMembers)

如有以下類

public class AnotherClass
{
        private int myPrvField1 = 15;
        private string myPrvField2 = "Some private field";
        public decimal myPubField1 = 1.03m;
}

假設你需要使用Type類的方法FindMembers來擷取AnotherClass執行個體上的私人欄位,並且顯示它們的值

代碼如下

FieldInfo fi;
AnotherClass ac = new AnotherClass();
MemberInfo[] memInfo = ac.GetType().FindMembers(MemberTypes.Field,BindingFlags.NonPublic | BindingFlags.Instance,null,null);
foreach(MemberInfo m in memInfo)
{
    fi = m as FieldInfo;
    if(fi != null)
    {
        Console.WriteLine("{0} of value:{1}",fi.Name,fi.GetValue(ac));
    }
}

 

 

 

聯繫我們

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