static未央

博客园 首页 新随笔 联系 订阅 管理
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;
 6 
 7 namespace attribute
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Tester t = new Tester();
14             t.CannotRun();
15 
16             Type tp = typeof(Tester);
17 
18             MethodInfo methodinfo = tp.GetMethod("CannotRun");
19             TestAttribute myatt = (TestAttribute)Attribute.GetCustomAttribute(methodinfo, typeof(TestAttribute));
20             myatt.RunTest();
21         }
22     }
23     class Tester
24     {
25         [Test("Error Here")]
26         public void CannotRun()
27         {
28             Console.WriteLine("fuck this is a error");
29         }
30     }
31     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true)]
32     public class TestAttribute : System.Attribute
33     {
34         public TestAttribute(string name)
35         {
36             Console.WriteLine(name);
37         }
38         public void RunTest()
39         {
40             Console.WriteLine("test here");
41         }
42     }
43 }
44 

 

posted on 2010-11-23 22:56  abstract未央  阅读(291)  评论(0编辑  收藏  举报