- 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
属性和反射
我们明白怎么创建attribtes和把它们绑定至语言元素。 该如何在运行时查询这信息? 为了查询一语言元素上绑定的attributes,必须使用反射 反射有能力在运行时发现类型信息 提供访问运行程序的元数据的类时在System.Reflection命名空间中 System.Reflection命名空间包含允许程序员获取关于运行的程序的信息的类,以动态的添加类型、值和对象到程序中 * 反射通常用于下面的任务: 浏览元数据 完成类型发现 方法和特性的后期绑定 反射发出 * 浏览元数据: 为了使用反射来浏览元数据,System.Reflection命名空间的MemberInfo对象需要被实例化。 这个对象有助于发现成员的属性并且提供对元数据的访问 Type type = typeof(AnyClass); 使用typeof操作符返回AnyClass对应的Type对象。要查询方法和域的属性,首先要获得当前类中所有方法和类,然后再用类似于查询类的属性的方法来查询与之对应的属性。 * using System; using System.Reflection; using System.Diagnostics; //将Help帮助信息附加到整个程序集上 [assembly: Help(This Assembly demonstrates custom attributes creation and their run-time query.)] public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { this.description = Description_in; } protected String description; public String Description { get { return this.description; } } } //将Help帮助信息附加到自定义的类AnyClass [Help(This is a do-nothing Class.)] public class AnyClass { //attaching Help attribute to our AnyMethod [Help(This is a do-nothing Method.)] public void AnyMethod() { } //attaching Help attribute to our AnyInt Field [Help(This is any Integer.)] public int AnyInt; } * class QueryApp { public static void Main() { HelpAttribute HelpAttr; //查询程序集对应的特性 String assemblyName; Process p = Process.GetCurrentProcess(); assemblyName = p.ProcessName + .exe; Console.WriteLine(当前运行的程序集名称:{0}\n所包含的类型定义为:, assemblyName); Assembly a = Assembly.LoadFrom(assemblyName); foreach (Type t in a.GetTypes()) Console.WriteLine(“Type:{0} Info:{1}”, t.GetType(),t.ToString()); Console.WriteLine(); //查询类AnyClass的相关信息 Type type = typeof(AnyClass); foreach (Attribute attr in type.GetCustomAttributes(false)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) Console.Write
文档评论(0)