您的位置:首页 > 编程语言 > C#

C#反射读取类定制特性信息

2013-10-16 13:00 169 查看
此项目又分为3个小项目:

1.WhatsNewAttributes类库文件:用于定义定制特性类,标示跟新信息

2.myClass类库文件:使用WhatsNew定制特性标记的类

3.Checker控制台应用程序:用反射读取myClass中的公共成员方法,以及定制特性



因此:myClass类库需引入WhatsNewAttributes类库编译的DLL文件

Checker控制台应用程序需引入myClass类库编译的DLL文件^-^,WhatsNewAttributes类库编译的DLL文件



WhatsNewAttributes类库文件

[c-sharp] view
plaincopy

using System;



namespace WhatsNewAttributes

{

/*

* 特性类本身用一个特性System.AttributeUsage来标记,只能用在特性上,不能应用到类上

* 该特性主要标示定制特性可应用到哪些类型的程序元素上

*

* AttributeTargets:指定定制特性可应用到哪些类型的程序元素上,可用"|"指定多个元素

* AllowMultiple参数:表示一个特性是否可多次应用到同一项上

* Inherited参数:表示应用到类,接口,方法,属性上的特性可自动应用到所有派生类,接口,方法,属性,重写版本上

*

* 特性类派生于System.Attribute类

*/



/***************** 定义特性类LastModifiedAttribute ******************/

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple=true, Inherited=false)]

public class LastModifiedAttribute : Attribute

{

private DateTime _dateModified;

private string _changes;

private string _issues;



//特性类构造函数

public LastModifiedAttribute(string dateModified, string changes)

{

this._dateModified = DateTime.Parse(dateModified);

this._changes = changes;

}



public DateTime DateModified

{

get { return _dateModified; }

}



public string Changes

{

get { return _changes; }

}



//特性类可选属性

public string Issues

{

get { return _issues; }

set { _issues = value; }

}

}



/***************** 定义特性类SupportsWhatsNewAttribute,指示该特性仅用于程序集 ******************/

[AttributeUsage(AttributeTargets.Assembly)]

public class SupportsWhatsNewAttribute : Attribute

{

}

}



myClass类库文件

[c-sharp] view
plaincopy

using System;

//添加创建特性类生成的引用,包含其命名空间,这样编译器才能识别这些特性

using WhatsNewAttributes;



//用SupportsWhatsNes特性标记程序集本身

[assembly : SupportsWhatsNew]

namespace myClass

{

//在类上使用定制特性

[LastModified("14 Feb 2008", "改良了XX功能1")]

[LastModified("16 Jun 2010", "改良了XX功能2")]

public class myClass

{

private string _my;



public string My

{

get { return _my; }

set { _my = value; }

}



//在方法上使用定制特性,添加可选属性Issues

[LastModified("17 Jun 2010", "改良:从写了基类的ToString()方法", Issues="这是个突出问题")]

public override string ToString()

{

return "新更新的重写方法";

}



//私有方法

private void myFun()

{ }

}

}



Checker控制台应用程序

[c-sharp] view
plaincopy

using System;

using System.Reflection;

//添加创建特性类生成的引用,包含其命名空间,这样编译器才能识别这些特性

using WhatsNewAttributes;



namespace Checker

{

class Checker

{

static void Main(string[] args)

{

/* Assembly类:在System.Reflection命名空间定义,允许访问给定程序集的元数据

* 1.使用Assembly实例前,需要加入相应程序集

* Assembly.Load():参数为程序集名称

* Assembly.LoadFrom():参数为程序集的完整路径名

* 2.查找在程序集中定义的类型Assembly.GetTypes()

* 3.查找程序集的定制特性Attribute.GetCustomAttributes(assemblyObj)

* 定义定制特性时,必须为他们编写类,定制特性与对象一样,加载了程序集后

* 就可以读取这些特定对象,查看他们的属性,调用他们的方法

* a.所有特性都作为一般Attribute引用来获取

* b.如要调用为定制特性定义的方法属性,就要把这些引用显示转化为相关定制特性类

*/

Assembly theAssembly = Assembly.Load("myClass");

Attribute supportsAttribute = Attribute.GetCustomAttribute(theAssembly,typeof(SupportsWhatsNewAttribute));



string Name = theAssembly.FullName;



if (supportsAttribute == null)

{

Console.WriteLine("此程序集不支持SupportsWhatsNew特性");

return;

}

else

{

Console.WriteLine("程序集:" + Name);

}



//Assembly类的特性可以获得在相应程序集中定义的所有类型的信息

//调用GetTypes()方法,获得此程序集中定义的类型数组,此处只定义了一个myClass类

Type[] types = theAssembly.GetTypes();


//注:此处如果只要同一项目下的某类的类型,可以直接使用typeof(ClassName)来代替

Console.WriteLine("信息总数:" + types.Length); //1



foreach (Type definedType in types)

{

//如果该definedType不是一个类

if (!(definedType.IsClass))

{

//退出程序

return;

}

else

{

Console.WriteLine("成员名:" + definedType.Name); //myClass

}



//检索myClass类的自定义属性数组

Attribute[] attribs = Attribute.GetCustomAttributes(definedType);



if (attribs.Length == 0)

{

Console.WriteLine("这个类没有改良过");

}

else

{

foreach (Attribute attrib in attribs)

{

WriteAttributeInfo(attrib);

}

}



//检索myClass类的所有公共方法数组

MemberInfo[] methods = definedType.GetMethods();

Console.WriteLine("修改过的方法:");



foreach (MethodInfo nextMethod in methods)

{

//检索myClass类的当前公共方法的由LastModifiedAttribute标示的定制属性数组

object[] attribs2 = nextMethod.GetCustomAttributes(typeof(LastModifiedAttribute), false);



if (attribs2 != null)

{

Console.WriteLine( nextMethod.ReturnType + " " + nextMethod.Name + "()");



foreach(Attribute nextAttrib in attribs2)

{

WriteAttributeInfo(nextAttrib);

}

}

}

}



Console.ReadLine();

}



//显示该自定义属性的信息

public static void WriteAttributeInfo(Attribute attrib)

{

//定义特性类LastModifiedAttribute就是为了方便实例化后访问类中个属性

LastModifiedAttribute lastModifiedAttrib = attrib as LastModifiedAttribute;



if (lastModifiedAttrib == null)

{

return;

}



DateTime modifiedDate = lastModifiedAttrib.DateModified;



Console.WriteLine("改良:" + modifiedDate.ToLongDateString() + "," + lastModifiedAttrib.Changes);



if (lastModifiedAttrib.Issues != null)

{

Console.WriteLine("突出问题:" + lastModifiedAttrib.Issues);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: