//-----------------------------------------------------------------------------------
反射常用于以下四个任务
(1) 浏览元数据
(2) 查询类型 :利用反射查看配件的类型,方法,性质,事件,以及和改类型相关的一切信息
(3) 与方法和性质迟绑定 (动态调用)
(4) 在运行时创建类型 (反射发送)
//-----------------------------------------------------------------------------------
// Programming C# by O'Reilly & Associates,Inc
//
//
//-----------------------------------------------------------------------------------
// 反射常用于以下四个任务
// (1) 浏览元数据
// (2) 查询类型 :利用反射查看配件的类型,方法,性质,事件,以及和改类型相关的一切信息
// (3) 与方法和性质迟绑定 (动态调用)
// (4) 在运行时创建类型 (反射发送)
//-----------------------------------------------------------------------------------
using System;
using System.Reflection;
using System.Collections;
namespace Programing_CSharp_Reflection
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//测试类
public class App
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MyMath mm = new MyMath();
int result = mm.Function_Sum(10,15);
Console.WriteLine("Result is {0}",result);
Console.WriteLine();
// (1)浏览元数据
System.Reflection.MemberInfo mbrInfo = typeof(MyMath);
object[] attributes = mbrInfo.GetCustomAttributes(typeof(BugFixAttribute),false);
foreach(object attribute in attributes)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BugFixAttribute bfa = (BugFixAttribute) attribute;
Console.WriteLine(" BugID :{0}",bfa.BugID);
Console.WriteLine("Progammer:{0}",bfa.Programmer);
Console.WriteLine("Date :{0}",bfa.Date);
Console.WriteLine("Comment :{0}",bfa.Comment);
}
// (2)查询类型
Assembly asb = Assembly.Load("Mscorlib.dll");
Type[] types = asb.GetTypes();
foreach(Type t in types)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Type is {0}",t);
MemberInfo[] mbrInfoArray = t.GetMembers();
//也可以查询一个类型的成员,甚至可以查找特定的成员,例如只查找"Get"打头的方法
// MemberInfo[] mbrInfoArray = t.FindMembers(MemberTypes.Method,BindingFlags.Default,Type.FilterName,"Get*")
foreach(MemberInfo mbr in mbrInfoArray)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0} IS A {1}",mbr,mbr.MemberType);
}
}
Console.WriteLine(types.Length);
Console.ReadLine();
}
}
// 自定义的属性类
public class BugFixAttribute : System.Attribute
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int bugID;
private string programmer;
private string date;
private string comment;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int BugID
{get
{return bugID;}set
{bugID=value;}}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Programmer
{get
{return programmer;}set
{programmer=value;}}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Date
{get
{return date;}set
{date=value;}}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Comment
{get
{return comment;} set
{comment=value;}}
//构造函数
public BugFixAttribute(int bugID,string programmer,string date)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.bugID = bugID;
this.programmer = programmer;
this.date = date;
}
}
//将属性应用到一个类上
[BugFixAttribute(100,"zh-bin@163.com","2004/07/31",Comment="添加加法运算")]
public class MyMath
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public int Function_Sum(int i,int j)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return i+j;
}
public int Function_Sub(int i,int j)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return i-j;
}
}
}
posted on
2004-07-31 15:20
xpoint
阅读(
441)
评论()
编辑
收藏
举报