欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于C#基礎(chǔ)知識回顧--反射(三)

 更新時間:2013年07月10日 10:44:38   作者:  
在前面例子中,由于MyClass類型的對象是顯示創(chuàng)建的,因此使用反射技術(shù)來調(diào)用MyClass上的方法沒有任何優(yōu)勢--以普通的方式調(diào)用對象上的方法會簡單的多

但是,如果對象是在運行時動態(tài)創(chuàng)建的,反射的功能就顯示出來了。在這種情況下,需要首先獲取一個構(gòu)造函數(shù)列表,然后再調(diào)用列表中的某個構(gòu)造函數(shù),創(chuàng)建一個該類型的實例。通過這種機制,可以在運行時實例化任意類型的對象而不必在聲明中指定。

為了獲得某個類型的構(gòu)造函數(shù),需要調(diào)用Type對象上的GetConstructors()。常用形式為:
ConstructorInfo[] GetConstructors()
該方法返回一個描述構(gòu)造函數(shù)的ConstructorInfo對象數(shù)組。ConstructorInfo中常用的
是GetParamters()方法,該方法返回給定構(gòu)造函數(shù)的參數(shù)列表。
一旦找到了合適的構(gòu)造函數(shù),就調(diào)用ConstructorInfo定義的Invoke()方法來創(chuàng)建對象:
object Invoke(object[] args)

需要傳遞給此方法的所有參數(shù)都在args中指定。如果不需要參數(shù),args必須為null。另外,
args必須包含與參數(shù)個數(shù)相同的元素,并且實參的類型必須與形參的類型兼容。Invoke()方法返回
的是指向新構(gòu)造對象的引用。
例子:
測試對象類

復(fù)制代碼 代碼如下:

class MyClass
{
    int x;
    int y;
    public MyClass(int i)
    {
        Console.WriteLine("一個參數(shù)的構(gòu)造函數(shù):");
        x = y = i;
    }
    public MyClass(int i, int j)
    {
        Console.WriteLine("兩個參數(shù)構(gòu)造函數(shù):");
        x = i;
        y = j;
        Show();
    }
    public int Sum()
    {
        return x + y;
    }
    public bool IsBetween(int i)
    {
        if (x < i && i < y)
            return true;
        else
            return false;
    }
    public void Set(int a, int b)
    {
        Console.Write("函數(shù):Set(int a, int b)");
        x = a;
        y = b;
        Show();
    }
    public void Set(double a, double b)
    {
        Console.Write("函數(shù):Set(double a, double b)");
        x = (int)a;
        y = (int)b;
        Show();
    }
    public void Show()
    {
        Console.WriteLine("x:{0},y:{1}", x, y);
    }
}

使用反射:
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Reflection
{
    class Program
    {
        static void Main(string[] args)
        {
            InvokeConsDemo();
            Console.ReadKey();
        }

        static void InvokeConsDemo()
        {
            Type t = typeof(MyClass);
            int val;
            ConstructorInfo[] ci = t.GetConstructors();
            Console.WriteLine("類構(gòu)造函數(shù)如下:");
            foreach (ConstructorInfo c in ci)
            {
                Console.Write("" + t.Name + "(");
                ParameterInfo[] pi = c.GetParameters();
                for (int i = 0; i < pi.Length; i++)
                {
                    Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name);
                    if (i + 1 < pi.Length) Console.Write(", ");
                }
                Console.WriteLine(") ");
            }
            Console.WriteLine();
            int x;
            for (x = 0; x < ci.Length; x++)
            {
                ParameterInfo[] pi = ci[x].GetParameters();
                if (pi.Length == 2) break;
            }
            if (x == ci.Length)
            {
                Console.WriteLine("沒有找到兩個參數(shù)的構(gòu)造函數(shù)"); return;
            }
            else
            {
                object[] consargs = new object[2];
                consargs[0] = 10;
                consargs[1] = 20;
                object reflectOb = ci[x].Invoke(consargs);
                Console.WriteLine("用reflectOb調(diào)用方法");
                Console.WriteLine();
                MethodInfo[] mi = t.GetMethods();
                foreach (MethodInfo m in mi)
                {
                    ParameterInfo[] pi = m.GetParameters();
                    if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(int))
                    {
                        object[] args = new object[2];
                        args[0] = 12;
                        args[1] = 7;
                        m.Invoke(reflectOb, args);
                    }
                    else if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(double))
                    {
                        object[] args = new object[2];
                        args[0] = 1.25;
                        args[1] = 7.5;
                        m.Invoke(reflectOb, args);
                    }
                    else if (m.Name.CompareTo("Sum") == 0)
                    {
                        val = (int)m.Invoke(reflectOb, null);
                        Console.WriteLine("Sum is {0}",val);
                    }
                    else if (m.Name.CompareTo("IsBetween") == 0)
                    {
                        object[] args = new object[1];
                        args[0] = 13;
                        if ((bool)m.Invoke(reflectOb, args))
                        {
                            Console.WriteLine("13 is between x and y");
                        }
                    }
                    else if (m.Name.CompareTo("Show") == 0)
                    {
                        m.Invoke(reflectOb, null);
                    }
                }
            }
        }
    }
}

運行結(jié)果為:



本例中,找到了一個兩個參數(shù)的構(gòu)造函數(shù),那么使用下面的語句實例化了一個該類型的對象:
object reflectOb=ci[x].Invoke(consargs);
調(diào)用Invoke()方法后,reflectOb將引用一個MyClass類型的對象。此后,程序?qū)?zhí)行
reflectOb上的方法。
注意:本例為了簡單起見,假設(shè)了一個使用兩個參數(shù)的構(gòu)造函數(shù),并且兩個參數(shù)都為int類型。但在實際的應(yīng)用程序中,必須檢驗每一個參數(shù)的類型。

相關(guān)文章

最新評論