自定义反射的示例

 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 

 

转载于:https://www.cnblogs.com/kakaliush/archive/2010/11/23/1885982.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值