C#学习笔记(十六):Attribute

Attribute可以为类或方法添加一些附加的信息,我们可以看看MSDN对Attribute的描述:

公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。

在.NET中,Attribute被用来处理多种问题,比如序列化、程序的安全特征、防止即时编译器对程序代码进行优化从而代码容易调试等等。

内置Attribute

C#为我们提供了一些用来处理特定一些问题,我们接下来看看几个简单的特性:

Conditional

满足指定条件才会调用指定的方法。

 1 using System;
 2 using System.Diagnostics;
 3 
 4 namespace Study
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Func();
11 
12             Console.Read();
13         }
14 
15         [Conditional("DEBUG")]
16         private static void Func()
17         {
18             Console.WriteLine("I am a DEBUG function!");
19         }
20     }
21 }

在DEBUG模式下运行可以看见字符串输出,而在RELEASE模式下Func不会被调用。

Obsolete

可以指定类或方法是否已经被弃用,第一个参数为描述字符串,第二参数可以指定使用了类或方法时是否直接抛错。

 1 using System;
 2 using System.Diagnostics;
 3 
 4 namespace Study
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Func();
11 
12             Console.Read();
13         }
14 
15         [Obsolete("Don`t use this method!", true)]
16         private static void Func()
17         {
18             Console.WriteLine("I am a DEBUG function!");
19         }
20     }
21 }

呵呵,自己运行看看效果吧。

Serializable

使用这个特性可以实现类的序列化和反序列化效果。

 1 using System;
 2 using System.IO;
 3 using System.Runtime.Serialization;
 4 using System.Runtime.Serialization.Formatters.Binary;
 5 
 6 namespace Study
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Test test = new Test {name = "LiLei", age = 18};
13 
14             byte[] bytes = ObjectToBytes(test);
15             Test test2 = BytesToObject(bytes) as Test;
16 
17             Console.WriteLine("name: " + test2.name + ", age: " + test2.age);
18 
19             Console.Read();
20         }
21 
22         public static byte[] ObjectToBytes(object obj)
23         {
24             using(MemoryStream ms = new MemoryStream())
25             {
26                 IFormatter formatter = new BinaryFormatter();
27                 formatter.Serialize(ms, obj);
28                 return ms.GetBuffer();
29             }
30         }
31 
32         public static object BytesToObject(byte[] Bytes)
33         {
34             using(MemoryStream ms = new MemoryStream(Bytes))
35             {
36                 IFormatter formatter = new BinaryFormatter();
37                 return formatter.Deserialize(ms);
38             }
39         }
40     }
41 
42     [Serializable]
43     public class Test
44     {
45         public string name;
46         public int age;
47     }
48 }

注意可以使用两个静态序列化方法的对象必须是使用了Serializable特性的对象。

自定义Attribute

除了使用官方提供的特性以外,我们也可以根据自身的需求自定义一个自己的特性,具体使用方法如下:

  1. 新建一个类继承自System.Attribute,名称需要以Attribute结尾,比如VersionAttribute,我们可以给该类添加我们需要的属性。
  2. 使用时可用“[VersionAttribute]”或“[Version]”定义一个Attribute,同时可以设置属性。
  3. 具体使用时一般是使用反射的方式把我们需要的信息取出再进行处理。

给出几个不错的文章:

http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html

http://www.cnblogs.com/luckdv/articles/1682488.html

转载于:https://www.cnblogs.com/hammerc/p/4615920.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值