源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

关于C#基础知识回顾--反射(二)

  • 时间:2021-05-15 00:12 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:关于C#基础知识回顾--反射(二)
[b]使用反射调用方法: [/b]一旦知道一个类型所支持的方法,就可以对方法进行调用。调用时,需使用包含在 MethodInfo中的Invoke()方法。调用形式: object Invoke(object ob, object[] args)   这里ob是一个对象引用,将调用它所指向的对象上的方法。对于静态方法,ob必须为null。 所有需要传递给方法的参数都必须在args数组中指定。如果方法不需要参数,则args必须为null。 另外,数组args的元素数量参数必须等于参数的数量。Invoke()方法返回被调用方法的返回值。 要调用某个方法,只需在一个MethodInfo实例上调用Invoke(),该实例通过调用 GetMethods() [b]方法获得。请看事例: [/b]
[u]复制代码[/u] 代码如下:
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);     } }
运行结果如下: [img]http://files.jb51.net/file_images/article/201307/2013071010343038.png[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部