在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
摘要 using System; public class AnyClass { [Obsolete("别用Old这个老方法了,请用New方法", true)] static void Old( ) { } static void New( ) { } public static void Main( ) { Old( ); } } 在这个例子里我们使用了Obsolete(“陈旧的”)属性,它会将其所修饰的程序实体(类、方法、数据成员等)说明为已废弃不用的。第一个参数—一个字符串说明这个实体为何被废弃、由谁代替。实际上这个字符串的内容你想写什么都可以。第二个参数则告诉编译器将用户对此实体的调用视作一个编译错误。这个参数的缺省值为false,表示编译器仅将用户对其的调用视作警告。编译上面这段代码时,我们将会得到一个编译错误(译注:注意编译错误后附的提示了吗?): AnyClass.Old() is obsolete:“别用Old这个老方法了,请用New方法” 开发自定义的属性 using System; public class HelpAttribute : Attribute { } 不管你相不相信,我们已经创建了一个自定义的属性。我们可以象这样用它修饰任何的类: [Help()] public class AnyClass { } 注意: using System; public class HelpAttribute : Attribute { public HelpAttribute(String Descrition_in) { this.description = Description_in; } protected String description; public String Description { get { return this.description; } } } [Help("这是个什么也不做的类")] public class AnyClass { } 定义/控制自定义属性的使用
让我们做点具体的吧。我们将会用一个AttributeUsage属性修饰我们的属性类,以控制其作用范围: using System; [AttributeUsage(AttributeTargets.Class), AllowMultiple = false, Inherited = false ] public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { this.description = Description_in; } protected String description; public String Description { get { return this.description; } } } 先看看AttributeTargets.Class,说明了我们的Help属性只能用以修饰类,下面的这段代码将会导致一个编译错误(“属性Help不能用在这样的声明上,它只能用在类的声明上”),因为我们用Help属性去修饰方法AnyMethod()了: [Help("this is a do-nothing class")] public class AnyClass { [Help("this is a do-nothing method")] //error public void AnyMethod() { } } 编译错误: AnyClass.cs: Attribute ''Help'' is not valid on this declaration type. It is valid on ''class'' declarations only. 当然我们可以AttributeTargets.All来允许Help属性修饰任何类型的程序实体。AttributeTargets可能的值包括:
接下来,该看看AllowMultiple = false这句了:它确定了不能象下面这样,在同一实体上同时使用多个同种属性进行修饰: [Help("this is a do-nothing class")] [Help("it contains a do-nothing method")] public class AnyClass { [Help("this is a do-nothing method")] //这也是错误的,因为Help属性只能修饰类 public void AnyMethod() { } } 编译错误: AnyClass.cs: Duplicate ''Help'' attribute 我们再来谈谈AttributeUsage的最后一个数据属性Inherited:定义了自定义属性的修饰是否可由被修饰类的派生类继承。基于下示代码表示的继承关系,让我们看看会发生什么吧: [Help("BaseClass")] public class Base { } public class Derive : Base { } 我们选择了AttributeUsage的四种组合:
对应上述组合的结果:
注意: [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { this.description = Description_in; this.verion = "No Version is defined for this class"; } protected String description; public String Description { get { return this.description; } } protected String version; public String Version { get { return this.version; } //即便我们不想我们的属性用户设置Version这个数据属性,我们也不得不提供set方法 set { this.verion = value; } } } [Help("This is Class1")] public class Class1 { } [Help("This is Class2", Version = "1.0")] public class Class2 { } [Help("This is Class3", Version = "2.0", Description = "This is do-nothing class")] public class Class3 { } 我们查询Class1的Help属性信息时,会得到下列结果: Help.Description : This is Class1 Help.Version :No Version is defined for this class 如果我们不定义Version数据属性的值,那么构造子函数体内所赋的缺省值将会使用。如果也没有,那么该数据属性对应的数据类型的缺省值将被使用(比如int类型的缺省值为0)。查询Class2将会得到这样的结果: Help.Description : This is Class2 Help.Version : 1.0 请不要为了可选的参数而提供多个构造子的重载版本,而应该将它们定义成命名参数。我们之所以称其为“命名的”,是我们为了能在构造子里给它们赋值,不得不用一个个的标识符定义和访问它们。比如在第二个类中Help属性的使用[Help("This is Class2", Version = "1.0")] 你瞧,AttributeUsage的ValidOn参数就是一个定位参数,而Inherited与AllowMultiple则是命名参数。 注意:在属性类的构造子中给命名参数赋值,我们必须为它提供一个相应的set方法,否则会导致这样的编译错误(Version不能是一个只读的数据属性): ''Version'' : Named attribute argument can''t be a read only property 当我们查询Class3的Help属性信息时会发生什么呢?会这样-因上述的原因导致编译错误(Description不能是只读的数据属性): ''Desciption'' : Named attribute argument can''t be a read only property 所以还是给Description添加一个set方法吧。这样会得到正确的输出结果: This is do-nothing class Help.Version : 2.0 这是因为构造子利用定位参数构造一个属性时,它会调用所有命名参数的set方法。构造子里的赋值行为实际均由各命名参数对应的数据属性的set方法完成,被其覆写(Override)了。
属性标识 [assembly: Help("this a do-nothing assembly")] 这个Help属性前的assembly标识符显式地告诉了编译器,当前这个Help属性用于整个程序集。可用的标识符包括:
在运行时查询属性 using System; using System.Reflection; using System.Diagnostics; //attaching Help attribute to entire assembly [assembly : Help("This Assembly demonstrates custom attributes creation and their run-time query.")] //our custom attribute class public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { this.description = Description_in; } protected String description; public String Description { get { return this.deescription; } } } //attaching Help attribute to our AnyClass [HelpString("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() { } } 我们将在接下来的两节里在我们的Main方法里加入属性查询的代码。 class QueryApp { public static void Main() { HelpAttribute HelpAttr; //Querying Assembly Attributes String assemblyName; Process p = Process.GetCurrentProcess(); assemblyName = p.ProcessName + ".exe"; Assembly a = Assembly.LoadFrom(assemblyName); foreach (Attribute attr in a.GetCustomAttributes(true)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of {0}:\n{1}", assemblyName,HelpAttr.Description); } } } } 程序的输出结果: Description of QueryAttribute.exe: This Assembly demonstrates custom attributes creation and their run-time query. Press any key to continue 查询类、方法和域的属性 Type type = typeof(AnyClass); 它使用typeof操作符返回AnyClass对应的Type对象。其余的代码也类似上述的代码,我想不需要再做解释了吧。要查询方法和域的属性,我们首先要获得当前类中所有方法和类,然后再用类似于查询类的属性的方法来查询与之对应的属性。 class QueryApp { public static void Main() { Type type = typeof(AnyClass); HelpAttribute HelpAttr; //Querying Class Attributes foreach (Attribute attr in type.GetCustomAttributes(true)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of AnyClass:\n{0}", HelpAttr.Description); } } //Querying Class-Method Attributes foreach(MethodInfo method in type.GetMethods()) { foreach (Attribute attr in method.GetCustomAttributes(true)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of {0}:\n{1}", method.Name, HelpAttr.Description); } } } //Querying Class-Field (only public) Attributes foreach(FieldInfo field in type.GetFields()) { foreach (Attribute attr in field.GetCustomAttributes(true)) { HelpAttr= attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of {0}:\n{1}", field.Name,HelpAttr.Description); } } } } } 下面是程序输出: Description of AnyClass: This is a do-nothing Class. Description of AnyMethod: This is a do-nothing Method. Description of AnyInt: This is any Integer. Press any key to continue
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论