导航

[转载]为什么要用到属性

Posted on 2012-01-31 17:20  Jessie.M  阅读(256)  评论(0编辑  收藏  举报
  1 using System;
2 using System.Reflection;
3
4 using 定义;
5 using 颁发者;
6 using 使用者;
7 using 程序;
8
9 namespace 定义
10 {
11 public interface IJob
12 {
13 void Work();
14 }
15 }
16
17 namespace 颁发者
18 {
19 //定义一个Slow的属性。它只能使用在方法上
20
21 [AttributeUsage(AttributeTargets.Method)]
22 public class SlowAttribute : Attribute
23 {
24 string desc;
25 public SlowAttribute(string waitdesc)
26 {
27 desc = waitdesc;
28 }
29 public string WaitDescription
30 {
31 get
32 {
33 return desc;
34 }
35 }
36 }
37 }
38
39 namespace 使用者
40 {
41 public class 入门DotNet : IJob
42 {
43 public void Work()
44 {
45 Console.WriteLine("--> 入门完毕 ");
46 }
47 }
48 public class 精通DotNet : IJob
49 {
50 [Slow("很久 ")]
51 public void Work()
52 {
53 System.Threading.Thread.Sleep(3000);
54 Console.WriteLine("........................精通完毕 ");
55 }
56 }
57 }
58
59 namespace 程序
60 {
61 public interface IJobExecutor
62 {
63 void Execute(IJob job);
64 }
65
66 class Util
67 {
68 static public MethodInfo FindJobWork(IJob job)
69 {
70 InterfaceMapping map = job.GetType().GetInterfaceMap(typeof(IJob));
71 return map.TargetMethods[0];
72 }
73 }
74
75 public class 浮躁的人 : IJobExecutor
76 {
77 public void Execute(IJob job)
78 {
79 MethodInfo mi = Util.FindJobWork(job);
80 bool isslow = mi.IsDefined(typeof(SlowAttribute), false);
81 if (!isslow)
82 {
83 job.Work();
84 Console.WriteLine("这么点小东西还难得到我? ");
85 }
86 else
87 {
88 Console.WriteLine("靠,这么麻烦,老子不学了!! ");
89 }
90 }
91 }
92 public class 耐心的人 : IJobExecutor
93 {
94 public void Execute(IJob job)
95 {
96 MethodInfo mi = Util.FindJobWork(job);
97 bool isslow = mi.IsDefined(typeof(SlowAttribute), false);
98 if (!isslow)
99 {
100 job.Work();
101 Console.WriteLine("努力,加油。 ");
102 }
103 else
104 {
105 SlowAttribute sa = (SlowAttribute)mi.GetCustomAttributes(typeof(SlowAttribute), false)[0];
106 Console.WriteLine("虽然做这个需要 " + sa.WaitDescription + ",但是我要把它完成。 ");
107 job.Work();
108 Console.WriteLine("哈哈。OK了 ");
109 }
110 }
111 }
112 }
113
114 namespace ConsoleApplication1
115 {
116 class Class1
117 {
118 [STAThread]
119 static void Main(string[] args)
120 {
121 IJob j1 = new 入门DotNet();
122 IJob j2 = new 精通DotNet();
123
124 IJobExecutor exec1 = new 浮躁的人();
125 IJobExecutor exec2 = new 耐心的人();
126
127 Console.WriteLine();
128 Console.WriteLine("广播:浮躁的人的: ");
129 Console.WriteLine();
130
131 Console.WriteLine("广播:入门: ");
132 exec1.Execute(j1);
133 Console.WriteLine("广播:精通: ");
134 exec1.Execute(j2);
135
136 Console.WriteLine();
137 Console.WriteLine("广播:耐心的人的: ");
138 Console.WriteLine();
139
140 Console.WriteLine("广播:入门: ");
141 exec2.Execute(j1);
142 Console.WriteLine("广播:精通: ");
143 exec2.Execute(j2);
144
145 Console.ReadLine();
146 }
147 }
148 }


原文:http://topic.csdn.net/t/20030624/15/1951393.html(21楼)

一段纠结的代码

 

属性是自定义的元数据的标记和表现形式。
属性的使用一般是被动的。
两个部分组成:
属性的颁发者和属性的使用者。

属性的颁发者声明了某种意义。要求属性的使用者把符合该意义的构造型(类,成员,,)标记上它。
那么属性的颁发者,就能通过根据它们是否有标记属性,和属性的数据进行某种操作了。

其中属性的颁发者是主动地,而属性的使用者是被动的。

对于平时,我们和我们创建的类只做为属性的使用者。

例如对于可序列化的对象,我们只是定义:
[Serilizable]   public   class   NameInfo
{
        private   string   n= "Undefined ";
        public   string   Name
        {
                get
                {
                        return   n;
                }
                set
                {
                        n=value;
                }
        }
}
又例如在WebService里用到的WebServiceAttribute和WebMethodAttribute

什么时候我们才做属性的颁布者,实现自己的属性?

对于一般的应用,属性是用不上的。通常的扩展都使用虚拟继承来实现。
只有当你做的东西有自己或其他人进行扩展时,才考虑使用自定义属性。