反射初探

来源:互联网 发布:量化投资 算法 编辑:程序博客网 时间:2024/05/17 06:54

这是一篇翻译文章。今天我很想写一篇如何实现银海医保接口的文章,可是我发现自己竟然不懂到底用到的是什么技术。这一定是很丢人的事情,我没有弄懂真正的问题所在,就开始刷刷写代码,实现接口,上线使用了。我用必应搜索了一下,原来我用到的是反射技术。反射是提供描述程序集、模块和类型的对象,可以动态的创建类型的实例,将类型绑定到存在的对象,或者从存在的对象中获取类型和调用它的方法、访问它的值域或者属性。【 https://msdn.microsoft.com/en-us/library/ms173183.aspx】。

以下是翻译文章:

Reflection in C# Tutorial

简介

在运行时,反射可以从托管代码的元数据里找到它的程序集、模块和类型。换言之,反射提供封装程序集、模块和类型的对象。程序的反射是通过从程序集中提取元数据,并使用元数据来通知用户或修改自己的行为来体现的。反射类似于C++的RTTI(Runtime Type Information,运行时类型信息),但是比RTTI范围更广和功能更强大。通过使用C#的反射,一方面可以找出对象和方法的细节,还能在运行时创建类型和方法。命名空间System.Reflection包含类和接口,接口支持加载类型 方法字段的托管视图还有具有动态创建和调用类型的能力。编写使用反射的 C# 代码编码器可以使用 typeof 运算符获取对象的类型使用GetType () 方法获取当前实例类型下面演示使用反射示例代码:

例1

using System;using System.Reflection;public class MyClass{   public virtual int AddNumb(int numb1,int numb2)   {     int result = numb1 + numb2;     return result;   }}class MyMainClass{  public static int Main()  {    Console.WriteLine ("\nReflection.MethodInfo");    // Create MyClass object    MyClass myClassObj = new MyClass();    // Get the Type information.    Type myTypeObj = myClassObj.GetType();    // Get Method Information.    MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb");    object[] mParam = new object[] {5, 10};    // Get and display the Invoke method.    Console.Write("\nFirst method - " + myTypeObj.FullName + " returns " +                           myMethodInfo.Invoke(myClassObj, mParam) + "\n");    return 0;  }}
首先,以下代码段获取type类型信息:

Type myTypeObj = Type.GetType("MyClass");
现在,myTypeObj已经获得了MyClass的信息了。因此,我们现在可以检查这个类是一个抽象类还是普通类,通过使用以下语句:

myTypeObj.IsAbstract 
或者:

myTypeObj.IsClass 
下面的代码段获取方法的信息,我们例子里的方法是AddNumb:

Methodinfo myMethodInfo = myTypeObj.GetMethod("AddNumb"); 
接下来的代码片段是调用AddNumb方法:

myMethodInfo.Invoke(myClassObj, mParam);//Example2: In this example, we will use the typeof keyword to obtain the//          System.Type object for a type.Public class MyClass2{  int answer;  public MyClass2()  {    answer = 0;  }  public int AddNumb(intnumb1, intnumb2)  {    answer = numb1 + numb2;    return answer;  }}

以下的代码片段获取 System.Type类型对象,这个对象的类型是MyClass2的:

Type type1 = typeof(MyClass2);

所以我们现在可以将 type1 对象传递 Activator.CreateInstance(type1) 方法,创建 type1 对象一个实例

object obj = Activator.CreateInstance(type1);
接下来,我们调用 MyClass2  AddNumb 方法,而且是使用之前创建的对象数组作为参数传递给AddNumb (int,int) 方法。

object[] mParam =newobject[] {5, 10}; 
最后我们通过方法名称 AddNumb 适当参数传递给 System.Type.InvokeMember()来完成调用 AddNumb (int,int) 方法

int res = (int)type1.InvokeMember("AddNumb", BindingFlags.InvokeMethod,null,                                  obj, mParam);//Here is the complete code:using System;using System.Reflection;namespace Reflection{   class Class1   {    public int AddNumb(int numb1, int numb2)    {      int ans = numb1 + numb2;      return ans;    }  [STAThread]  static void Main(string[] args)  {     Type type1 = typeof(Class1);      //Create an instance of the type     object obj = Activator.CreateInstance(type1);     object[] mParam = new object[] {5, 10};     //invoke AddMethod, passing in two parameters     int res = (int)type1.InvokeMember("AddNumb", BindingFlags.InvokeMethod,                                        null, obj, mParam);     Console.Write("Result: {0} \n", res);   }  }}

谢谢原文作者

Idemudia Sanni Esangbedo









0 0
原创粉丝点击