c# 高级主题: attribute,反射,多线程

上传人:第*** 文档编号:31072416 上传时间:2018-02-04 格式:DOC 页数:27 大小:394KB
返回 下载 相关 举报
c# 高级主题: attribute,反射,多线程_第1页
第1页 / 共27页
c# 高级主题: attribute,反射,多线程_第2页
第2页 / 共27页
c# 高级主题: attribute,反射,多线程_第3页
第3页 / 共27页
c# 高级主题: attribute,反射,多线程_第4页
第4页 / 共27页
c# 高级主题: attribute,反射,多线程_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《c# 高级主题: attribute,反射,多线程》由会员分享,可在线阅读,更多相关《c# 高级主题: attribute,反射,多线程(27页珍藏版)》请在金锄头文库上搜索。

1、C# 高级 主题一 自定义特性(Attribute)应用二 反射的应用三 多线程的应用Prepared by 刘富凰2007-11-072009-10 加上 .C#3.5 的内容一 自定义特性(Attribute)Attribute 概述Attribute 是一个类,下面是 msdn 文档对它的描述:公共语言运行时允许你添加类似关键字的描述声明,叫做 attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes 和 Microsoft .NET Framework 文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为

2、。在.NET 中,Attribute 被用来处理多种问题,比如序列化、程序的安全特征、防止即时编译器对程序代码进行优化从而代码容易调试等等。Attribute 作为编译器的指令在 C#中存在着一定数量的编译器指令,如:#define DEBUG, #undefine DEBUG, #if 等。这些指令专属于 C#,而且在数量上是固定的。而 Attribute 用作编译器指令则不受数量限制。比如下面的 4 个Attribute: Conditional:起条件编译的作用,只有满足条件,才允许编译器对它的代码进行编译。一般在程序调试的时候使用。 DllImport:用来标记非.NET 的函数,表明

3、该方法在一个外部的 DLL 中定义。 Obsolete:这个属性用来标记当前的方法已经被废弃,不再使用了。Serializable: 指示一个类可以序列化。需要保存在 ViewState 中的对象需要可序列化,当 Session变量要保存在数据库中时,也需要该 session 变量表示的对象是可序列化的。Attribute 类构造函数:protected Attribute(): 保护的构造器,只能被 Attribute 的派生类调用。三个静态方法: static Attribute GetCustomAttribute():这个方法有 8 种重载的版本,它被用来取出施加在类成员上指定类型的

4、Attribute。 static Attribute GetCustomAttributes(): 这个方法有 16 种重载版本,用来取出施加在类成员上指定类型的 Attribute 数组。 static bool IsDefined():由八种重载版本,看是否指定类型的定制 attribute 被施加到类的成员上面。实例方法: bool IsDefaultAttribute(): 如果 Attribute 的值是默认的值,那么返回 true。 bool Match():表明这个 Attribute 实例是否等于一个指定的对象。公共属性: TypeId: 得到一个唯一的标识,这个标识被用来区

5、分同一个 Attribute 的不同实例。我们可以自定义我们自己的 Attribute,所有自定义的 Attribute 必须直接或者间接地从 Attribute 这个类派生,如: public MyCustomAttribute : Attribute . Attribute 的命名规则: Attribute 的类名+Attribute, 引号中的 Attribute 是可选的。AttributeUsage 类可以通过 AttributeUsage 的 Attribute 来限定你的 Attribute 所施加的元素的类型。代码形式如下: AttriubteUsage(参数设置) publi

6、c 自定义 Attribute : Attribute . AttributeUsage 本身也是一个 Attribute,这是专门施加在 Attribute 类的 Attribute.public enum AttributeTargetsAll=16383,Assembly=1,Module=2,Class=4,Struct=8,Enum=16,Constructor=32,Method=64,Property=128,Field=256,Event=512,Interface=1024,Parameter=2048,Delegate=4096,ReturnValue=8192 作为参数的

7、 AttributeTarges 的值允许通过“或”操作来进行多个值得组合,如果你没有指定参数,那么默认参数就是 All 。 AttributeUsage 除了继承 Attribute 的方法和属性之外,还定义了以下三个属性:AllowMultiple: 读取或者设置这个属性,表示是否可以对一个程序元素施加多个 Attribute 。 Inherited:读取或者设置这个属性,表示是否施加的 Attribute 可以被派生类继承或者重载。 ValidOn: 读取或者设置这个属性,指明 Attribute 可以被施加的元素的类型。 AttributeUsage 的使用例子: using Syst

8、em; namespace AttTargsCS / 该 Attribute 只对类有效. AttributeUsage(AttributeTargets.Class)public class ClassTargetAttribute : Attribute / 该 Attribute 只对方法有效. AttributeUsage(AttributeTargets.Method)public class MethodTargetAttribute : Attribute / 该 Attribute 对类或者方法有效(组合). AttributeUsage(AttributeTargets.Cl

9、ass|AttributeTargets.Method)public class ClassMethodTargetAttribute : Attribute / 该 Attribute 对所有的元素有效.AttributeUsage(AttributeTargets.All)public class AllTargetsAttribute : Attribute /上面定义的 Attribute 施加到程序元素上的用法ClassTarget /施加到类ClassMethodTarget /施加到类AllTargets /施加到类public class TestClassAttribute

10、AllTargets /施加到构造器TestClassAttribute() MethodTarget /施加到方法ClassMethodTarget /施加到方法public void Method1() AllTargets /施加到字段public int myInt; static void Main(string args) .net Framework 中 SerializableAttribute 的声明:ComVisibleAttribute(true) AttributeUsageAttribute(AttributeTargets.Class|AttributeTarget

11、s.Struct|AttributeTargets.Enum|AttributeTargets.Delegate, Inherited=false) public sealed class SerializableAttribute : Attribute以下特性説明的意義:System.Web.Services.Protocols.SoapDocumentMethodAttribute(http:/tempuri.org/CheckUserPrivilege, RequestNamespace=http:/tempuri.org/, ResponseNamespace=http:/tempu

12、ri.org/, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)“http:/tempuri.org/CheckUserPrivilege”是特性 SoapDocumentMethodAttribute 的構造函數的參數,稱爲定位參數,是必須的,RequestNamespace 等是屬性,是可選的。取出我们需要的 Attribute 数据public static Attribu

13、te GetCustomAttribute (MemberInfo element, Type attributeType)public static Attribute GetCustomAttribute (Assembly element,Type attributeType)这是通过.NET 的反射来实现的.using System;using System.Reflection;namespace IsDef4CS public class TestClass Obsolete(该方法已经被废弃.请使用 Method2 来代替.)public void Method1()public

14、 void Method2() public class DemoClass static void Main(string args) / 要访问它的元数据(metadata) ,现取得它的类型Type clsType = typeof(TestClass);/ 取得描述 Method1 方法的 MethodInfo 对象MethodInfo mInfo = clsType.GetMethod(Method1);/ 看该方法是否定义了 Obsolete 特性bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute);/

15、显示结果Console.WriteLine(The Obsolete Attribute 0 defined for 1 of class 2.,isDef ? is : is not, mInfo.Name, clsType.Name);/ 如果定义了,就显示特性的消息if (isDef) ObsoleteAttribute obsAttr = (ObsoleteAttribute)Attribute.GetCustomAttribute(mInfo, typeof(ObsoleteAttribute);if (obsAttr != null)Console.WriteLine(消息是: 0.,obsAttr.Message);elseConsole.WriteLine(无法取得消息.);/* 输出:* The Obsolete Attribute is defined for Method1 of class TestClass.* 消息是: 该方法已经被废弃.请使用 Method2 来代替.*/二 反射的应用概述 反射的定义:审查元数据并收集关于它的类型信息的能力。元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等, 。System

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号