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#程序设计》