第六部分 属性2---------------

来源:互联网 发布:郑州网络布线公司 编辑:程序博客网 时间:2024/05/01 22:17

using System;

namespace MethodAttribs
{

 public class TransactionableAttribute:Attribute
 {
  public TransactionableAttribute()
  {}
 }
 /// <summary>
 /// SomeClass 的摘要说明。
 /// </summary>
 ///
 class SomeClass
 {
  [Transactionable]
  public void Foo()
  {}
  public void Bar()
  {}
  [Transactionable]
  public void Goo()
  {}
 }
  class Test
  {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   Type type=Type.GetType("MethodAttribs.SomeClass");
   foreach(MethodInfo method in type.GetMethods())
   {
    foreach(Attribute attr in method.GetCustomAttributes(true))
    {
     if(attr is TransactionableAttrubute)
     {

      Console.WriteLine("{0} is transactionable.",method.Name);
     }

    }
   
   }
  }
 }
}
<c#精髓>上的代码又出现问题,很有挫折感,MethodInfo以及Method类都没有给出....

感觉代码挺精妙的,但是实现不了,郁闷.................. 

原创粉丝点击