国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

關于C#基礎知識回顧--反射(二)

2020-01-24 03:12:20
字體:
來源:轉載
供稿:網友

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

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

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

要調用某個方法,只需在一個MethodInfo實例上調用Invoke(),該實例通過調用
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();

                //顯示參數
                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);
    }
}

運行結果如下:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涞水县| 甘南县| 太谷县| 秀山| 洛浦县| 东莞市| 霍邱县| 博爱县| 凯里市| 绵阳市| 慈利县| 邯郸市| 青州市| 太和县| 怀柔区| 西丰县| 民权县| 霸州市| 东安县| 葵青区| 江源县| 柳江县| 天镇县| 广丰县| 水城县| 罗田县| 会泽县| 漾濞| 探索| 普安县| 上杭县| 太仆寺旗| 孝义市| 河北省| 琼结县| 浦北县| 吴旗县| 晋州市| 建平县| 鄂尔多斯市| 金昌市|