C#基础知识回顾-- 反射(2)

使用反射调用方法:
一旦知道一个类型所支持的方法,就可以对方法进行调用。调用时,需使用包含在
 
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(1020);

            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);
    }
}
运行结果如下:

 

原文地址:https://www.cnblogs.com/smiler/p/3177097.html