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

源码网商城

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

  • 时间:2020-12-14 03:49 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:关于C#基础知识回顾--反射(三)
但是,如果对象是在运行时动态创建的,反射的功能就显示出来了。在这种情况下,需要首先获取一个构造函数列表,然后再调用列表中的某个构造函数,创建一个该类型的实例。通过这种机制,可以在运行时实例化任意类型的对象而不必在声明中指定。 为了获得某个类型的构造函数,需要调用Type对象上的GetConstructors()。常用形式为: ConstructorInfo[] GetConstructors() 该方法返回一个描述构造函数的ConstructorInfo对象数组。ConstructorInfo中常用的 是GetParamters()方法,该方法返回给定构造函数的参数列表。 一旦找到了合适的构造函数,就调用ConstructorInfo定义的Invoke()方法来创建对象: object Invoke(object[] args) 需要传递给此方法的所有参数都在args中指定。如果不需要参数,args必须为null。另外, args必须包含与参数个数相同的元素,并且实参的类型必须与形参的类型兼容。Invoke()方法返回 的是指向新构造对象的引用。 [b]例子: [/b]测试对象类
[u]复制代码[/u] 代码如下:
class MyClass {     int x;     int y;     public MyClass(int i)     {         Console.WriteLine("一个参数的构造函数:");         x = y = i;     }     public MyClass(int i, int j)     {         Console.WriteLine("两个参数构造函数:");         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("函数:Set(int a, int b)");         x = a;         y = b;         Show();     }     public void Set(double a, double b)     {         Console.Write("函数:Set(double a, double b)");         x = (int)a;         y = (int)b;         Show();     }     public void Show()     {         Console.WriteLine("x:{0},y:{1}", x, y);     } }
[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)         {             InvokeConsDemo();             Console.ReadKey();         }         static void InvokeConsDemo()         {             Type t = typeof(MyClass);             int val;             ConstructorInfo[] ci = t.GetConstructors();             Console.WriteLine("类构造函数如下:");             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("没有找到两个参数的构造函数"); return;             }             else             {                 object[] consargs = new object[2];                 consargs[0] = 10;                 consargs[1] = 20;                 object reflectOb = ci[x].Invoke(consargs);                 Console.WriteLine("用reflectOb调用方法");                 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);                     }                 }             }         }     } }
运行结果为: [img]http://files.jb51.net/file_images/article/201307/2013071010392939.png[/img] 本例中,找到了一个两个参数的构造函数,那么使用下面的语句实例化了一个该类型的对象: object reflectOb=ci[x].Invoke(consargs); 调用Invoke()方法后,reflectOb将引用一个MyClass类型的对象。此后,程序将执行 reflectOb上的方法。 注意:本例为了简单起见,假设了一个使用两个参数的构造函数,并且两个参数都为int类型。但在实际的应用程序中,必须检验每一个参数的类型。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部