反射

反射通常用于以下四种任务中:

1、浏览元数据
//使用反射
 public class Tester
 {
  public static void Main()
  {
   MyMath mm=new MyMath();
   Console.WriteLine("Calling DoFunc1(7). Result:{0}",mm.DoFunc1(7));

   System.Reflection.MemberInfo inf=typeof(MyMath); //Type类是访问元数据的主要方式
   object[] attributes;
   attributes=inf.GetCustomAttributes(typeof(BugFixAttribute),true);//传入要查询的属性信息的类型
   foreach(Object attribute in attributes)
   {
    BugFixAttribute bfa=(BugFixAttribute)attribute;
    Console.WriteLine("/nBugID:{0}",bfa.BugID);
    Console.WriteLine("Programmer:{0}",bfa.Programmer);
    Console.WriteLine("Date:{0}",bfa.Date);
    Console.WriteLine("Comment:{0}",bfa.Comment);
   }
   Console.ReadLine();
  }
 }
}

2、查询类型
A.反射一个配件
namespace Programming_CSharp
{
 using System;
 using System.Reflection;

 public class Test
 {
  public static void Main()
  {
   Assembly a=Assembly.Load("Mscorlib.dll");
   Type[] types=a.GetTypes();//Type代表着类型声明:类、接口、数组、值和枚举
   foreach(Type type in types)
   {
    Console.WriteLine("Type is {0}",type);
   }
   Console.WriteLine("{0} types found",types.Length);
   Console.ReadLine();
  }
 }
}

B.反射一个类型
public class Tester
 {
  public static void Main()
  {
   Type theType=Type.GetType("System.Reflection.Assembly");//用GetType()方法提取一个类型
   Console.WriteLine("/nSingle Type is {0} /n",theType);
   Console.ReadLine();
  }
 }

C.反射一个类型的成员

 public class Tester
 {
  public static void Main()
  {
   Type theType=Type.GetType("System.Reflection.Assembly");//用GetType()方法提取一个类型
   Console.WriteLine("/nSingle Type is {0} /n",theType);

   MemberInfo[] mbrInfoArray=theType.GetMembers();//列出所有方法、字段、属性
   foreach(MemberInfo mbrInfo in mbrInfoArray)
   {
    Console.WriteLine("{0} is a {1}",mbrInfo,mbrInfo.MemberType);
   }
   Console.ReadLine();
  }
 }

D.寻找类型方法

有时我们只关注方法,不需要字段、性质等。

 public class Tester
 {
  public static void Main()
  {
   Type theType=Type.GetType("System.Reflection.Assembly");//用GetType()方法提取一个类型
   Console.WriteLine("/nSingle Type is {0} /n",theType);

   //MemberInfo[] mbrInfoArray=theType.GetMembers(BindingFlags.LookupAll);//列出所有方法、字段、属性
   MemberInfo[] mbrInfoArray=theType.GetMethods();
   foreach(MemberInfo mbrInfo in mbrInfoArray)
   {
    Console.WriteLine("{0} is a {1}",mbrInfo,mbrInfo.MemberType);
   }
   Console.ReadLine();
  }
 }
E.查找特定成员

 public class Tester
 {
  public static void Main()
  {
   Type theType=Type.GetType("System.Reflection.Assembly");
   MemberInfo[] mbrInfoArray=theType.FindMembers(MemberTypes.Method,
    BindingFlags.Public |
    BindingFlags.Static |
    BindingFlags.NonPublic |
    BindingFlags.Instance |
    BindingFlags.DeclaredOnly,
    Type.FilterName,"Get*");
   foreach(MemberInfo mbrInfo in mbrInfoArray)
   {
    Console.WriteLine("{0} is a {1}",mbrInfo,mbrInfo.MemberType);
   }
   Console.ReadLine();
  }
 }

3、 //动态调用一个方法
 public class Tester
 {
  public static void Main()
  {
   Type theMathType=Type.GetType("System.Math");
   // Since System.Math has no public constructor, this
   // would throw an exception.
   //Object theObj=Activator.CreateInstance(theMathType);

   Type[] paramType=new Type[1];
   paramType[0]=Type.GetType("System.Double");

   MethodInfo cosineInfo=theMathType.GetMethod("Cos",paramType);
   
   Object[] parameters=new object[1];
   parameters[0]=45 * (Math.PI/180);

   //Object retValue=cosineInfo.Invoke(theObj,parameters);
   Object retValue=cosineInfo.Invoke(theMathType,parameters);

   Console.WriteLine("The cosine of a 45 degree angle {0}", retValue);
   Console.ReadLine();

  }
 }
4、发送反射

//代码来自《C#程序设计》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值