亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

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

class MyClass
{
    int x;
    int y;
    public MyClass(int i)
    {
        Console.WriteLine("一個(gè)參數(shù)的構(gòu)造函數(shù):");
        x = y = i;
    }
    public MyClass(int i, int j)
    {
        Console.WriteLine("兩個(gè)參數(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("類(lèi)構(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("沒(méi)有找到兩個(gè)參數(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);
                    }
                }
            }
        }
    }
}

運(yùn)行結(jié)果為:



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

相關(guān)文章

最新評(píng)論