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

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

 更新時間:2013年07月10日 10:38:02   作者:  
其實說白了,反射就是能知道我們未知類型的類型信息這么一個東西.沒什么神秘可講!反射的核心是System.Type。System.Type包含了很多屬性和方法,使用這些屬性和方法可以在運行時得到類型信息

使用反射調(diào)用方法:
一旦知道一個類型所支持的方法,就可以對方法進行調(diào)用。調(diào)用時,需使用包含在
MethodInfo中的Invoke()方法。調(diào)用形式:
object Invoke(object ob, object[] args)
 
這里ob是一個對象引用,將調(diào)用它所指向的對象上的方法。對于靜態(tài)方法,ob必須為null。

所有需要傳遞給方法的參數(shù)都必須在args數(shù)組中指定。如果方法不需要參數(shù),則args必須為null。

另外,數(shù)組args的元素數(shù)量參數(shù)必須等于參數(shù)的數(shù)量。Invoke()方法返回被調(diào)用方法的返回值。

要調(diào)用某個方法,只需在一個MethodInfo實例上調(diào)用Invoke(),該實例通過調(diào)用
GetMethods()

方法獲得。請看事例:

復制代碼 代碼如下:

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

        static void InvokeMethDemo()
        {
            //獲得MyClass的類型隊形
            Type t = typeof(MyClass);
            MyClass reflectOb = new MyClass(10, 20);
            Console.WriteLine("類名:{0}", t.Name);
            Console.WriteLine("本類所提供的方法有:");
            MethodInfo[] mi = t.GetMethods();
            int val;
            foreach (MethodInfo m in mi)
            {
                Console.WriteLine();

                //顯示參數(shù)
                ParameterInfo[] pi = m.GetParameters();
                if (m.Name == "Set" && pi[0].ParameterType == typeof(int))
                {
                    Console.Write("Set(int,int)  ");
                    object[] args = new object[2];
                    args[0] = 9;
                    args[1] = 18;
                    m.Invoke(reflectOb, args);
                }
                else if (m.Name == "Set" && pi[0].ParameterType == typeof(double))
                {
                    Console.Write("Set(double,double)  ");
                    object[] args = new object[2];
                    args[0] = 2.34;
                    args[1] = 13.56;
                    m.Invoke(reflectOb, args);
                }
                else if (m.Name.CompareTo("Sum") == 0) {
                    Console.Write("Sum() ");
                    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] = 17;
                    if ((bool)m.Invoke(reflectOb, args))
                    {
                        Console.WriteLine("{0}在x和y之間",args[0]);
                    }
                }
                Console.WriteLine();
            }
        }
    }
}
class MyClass
{
    int x;
    int y;
    public MyClass(int i, int j)
    {
        x = i;
        y = j;
    }
    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)
    {
        x = a;
        y = b;
        Show();
    }
    public void Set(double a, double b)
    {
        x = (int)a;
        y = (int)b;
        Show();
    }
    public void Show()
    {
        Console.WriteLine("x:{0},y:{1}", x, y);
    }
}

運行結(jié)果如下:


相關(guān)文章

最新評論