反射

反射:运行时发现类型的信息,创建类型的实例以及访问类型的成员。

 

1、通过反射可以读取物理文件程序集的代码内容。

2、可以通过反射,根据类名(字符串)动态的创建类的对象。

3、可以在程序运行时,动态地获得对象里的信息。(方法、属性、字段、索引)

4、可以根据成员名称(字符串)来调用执行对象里对应的成员。

 

Assembly类是对Assembly的描述

AppDomain.CurrentDomain.GetAssemblies()//获得正在运行的所有程序集。

Assembly.LoadFile()动态从文件加载Assembly,不需要再编译时引用。

//加载指定路径dll

Assembly ase = Assembly.LoadFrom(@"Model.dll");

 

public class Person
{
private string strName = "aa";
private int age = 18;
public int Age
{get{return age;}set{age = value;}}

public string StrName
{
get
{return strName;}

set
{strName = value;}
}

public string SayHi(string str)
{return strName + "大家好!"+str;}

}

 

Type用来保存某个类里成员信息的集合。

获取Type两种方式:

Type t1=typeof(类型);

Type t2=对象.GetType();

 

Type type=typeof(Person);

FieldInfo[] fields=type.GetFields();//获得类里所有的字段信息(字段名、字段类型,没有字段的值)
MethodInfo[] methods=type.GetMethods();//获得类里所有的方法信息
PropertyInfo[] pros=type.GetProperties();//获得类里所有的属性信息
type.GetConstructor();//获得构造函数

type.GetEvents();//获得事件

 

public class testFather
{
public string aa { get; set; }
}

interface IRun
{

}

public class testSun1
{
}

public class testSun:testFather,IRun
{
public string name { get; set; }

public string Shout()
{
return "shout";
}

public string Shout(string nameObj)
{
return name + " 参数:" + nameObj;
}

public string Shout(string nameObj,int intObj)
{
return string.Format("{0} 参数1:{1} 参数2:{2}", name, nameObj, intObj);
}

private string priFunc()
{
return "私有方法";
}

}

 

//IsAssignableFrom 判断testSun是否继承于testFather(类和接口都可以),实质是判断testSun是否可强转成testFather

MessageBox.Show( typeof(testFather).IsAssignableFrom(typeof(testSun)).ToString() );

Type typeFather= typeof(testFather);
Type typeSun = typeof(testSun);
Type typeSun1 = typeof(testSun1);
Type typeInter = typeof(testInterfance);
Type typeIRun = typeof(IRun);

testFather father = new testFather();
testSun sun = new testSun();
testSun1 sun1 = new testSun1();

 

string isAssign = typeFather.IsAssignableFrom(typeSun).ToString();
string isAssignFromInterfance = typeIRun.IsAssignableFrom(typeSun).ToString();
string isAssignFromInterfance1 = typeIRun.IsAssignableFrom(typeSun1).ToString();
MessageBox.Show(string.Format("{0},{1},{2}",isAssign,isAssignFromInterfance,isAssignFromInterfance1 ) );

 

//IsInstanceOfType 判断某个对象是否是某个type所对应的实例

bool isInstance = typeSun.IsInstanceOfType(sun);
bool isInstance1 = typeSun.IsInstanceOfType(father);
MessageBox.Show(string.Format("{0},{1}", isInstance, isInstance1));

 

//IsSubclassOf 判断某个类是否是另一个类的子类,只能用来判断类的继承,不能用来判断接口。

bool isSubF= typeSun.IsSubclassOf(typeFather);
bool isSubS = typeSun1.IsSubclassOf(typeFather);
bool isS = typeInter.IsSubclassOf(typeIRun);
MessageBox.Show(string.Format("{0},{1},{2}", isSubF, isSubS,isS));

 

//获取类里的方法

MethodInfo method = typeSun.GetMethod("Shout");

//执行方法
MessageBox.Show(method.Invoke(sun, null).ToString() ); 

 

//通过方法参数的类型数组来区分重载

//获得不带参数的Shout方法
MethodInfo method1 = typeSun.GetMethod("Shout",new Type[0]);
MessageBox.Show(method1.Invoke(sun, null).ToString()); 

//获得带一个参数的Shout方法
MethodInfo methodWidthPara = typeSun.GetMethod("Shout", new Type[] { typeof(string) } );
string str=methodWidthPara.Invoke(sun,new object[] { "123"}).ToString() ;
MessageBox.Show(str);

//调用私有方法
MethodInfo methodPrivate = typeSun.GetMethod("priFunc",BindingFlags.NonPublic|BindingFlags.Instance);
MessageBox.Show(methodPrivate.Invoke(sun,null).ToString() );

 

//获取自定义标签

[AttributeUsage(AttributeTargets.Property)]
public class testAttribute : Attribute
{
public bool isDisplay;
}

 

public class Person

{

[testAttribute(isDisplay =true)]

public string Name{get;set;}

[testAttribute(isDisplay =false)]

public int Age{get;set;}

public string Test{get;set;}

}

//使用

Person per=new Person();

ProertyInfo[] infos=typeof(Person).GetProperties();

foreach(PropertyInfo item in infos)

{

//IsDefined 是否为指定类型的自定义特性随即应用于指定的成员

if(item.IsDefined(typeof(testAttribute)))

{

//得到客户端标签isDisplay的值

if(((testAttribute)item.GetCustomAttribute(typeof(testAttribute))).isDisplay)

{

MessageBox.Show(item.GetValue(per).ToString());

}

}

}

 

//获取枚举描述

public enum testEnum
{
[Description("nromal描述")]
normal=0,
[Description("cal描述")]
cal =1,
[Description("rate描述")]
rate =2
}

//使用

Array array=typeof(testEnum).GetEnumValues();

foreach(var item in array)

{

FieldInfo field = Enum.Parse(typeof(testEnum),item.ToString()).GetType().GetField(item.ToString());

string des=((DescriptionAttribute)field.GetCustomAttribute(typeof(DescriptionAttribute))).Description;

MessageBox.Show(des);

}

 

posted @ 2017-05-07 16:49  jechsky  阅读(144)  评论(0编辑  收藏  举报