Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 7-15 extracted from chapter Reflection, late binding, attributes


Listing 7-14<     > Listing 7-16


This listing can be compiled with the command line:
csc.exe /target:library Example_7_15.cs
Errors: 0 Warnings: 0


Example_7_15.cs
using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.Method,AllowMultiple=false)]
public class TestAttribute : System.Attribute {
   public TestAttribute() { 
      Console.WriteLine("TestAttribute.ctor() Default ctor.");
   }
   public TestAttribute(int nTime) {
      Console.WriteLine("TestAttribute.ctor(int)");
      m_nTime = nTime;
   }
   private int m_nTime = 1;
   private bool m_Ignore = false;
   public bool Ignore{ get return m_Ignore; set { m_Ignore = value; } }
   public int  nTime { get return m_nTime; }  set { m_nTime = value; } }
}

class Program {
   static void Main() {
      // The programs reflects on itself.
      TestAssembly( Assembly.GetExecutingAssembly() );
   }
   static void TestAssembly(Assembly assembly) {
      // For each methods of each type declared in 'assembly'.
      foreachType type in assembly.GetTypes() ) {
         foreachMethodInfo method in type.GetMethods() ) {
            // Get attributes of type 'TestAttribute' which mark
            // the method. Trigger the call to 'TestAttribute.ctor()'.
            object[] attributes = method.GetCustomAttributes(
                                         typeof(TestAttribute),false);
            if( attributes.Length == ) {
               // Get an instance of'TestAttribute'.
               TestAttribute testAttribute =attributes[0as TestAttribute;
               // If we shouldn't ignore the method.
               if( ! testAttribute.Ignore ) {
                  object [] parameters = new object[0];
                  object instance = Activator.CreateInstance(type);
                  // Call the method 'nTime' times.
                  for(int i=0;i< testAttribute.nTime ; i++) {
                     try {
                       //Invoke the method with a late binds.
                       method.Invoke(instance,parameters);
                     catchTargetInvocationException ex ) {
                       Console.WriteLine(
                              "The method {" + type.FullName + "." + 
                              method.Name +
                              "} threw an exception of type " + 
                              ex.InnerException.GetType() +
                              " during run #" + (i+1) + ".");
                     }// end catch(...
                  }// end for(...
               }// end if( ! attribute.Ignore )
            }// end if( attributes.Length == 1 )
         }// end foreach(MethodInfo...
      }// end foreach(Type...
   }
}

class Foo {
   [Test()]
   public void Crash() {
      Console.WriteLine("Crash()");
      throw new ApplicationException();
   }
   int state = 0;
   [Test(4)]
   public void CrashTheSecondTime() {
      Console.WriteLine("CrashTheSecondTime()");
      state++;
      if (state == 2throw new ApplicationException();
   }
   [Test()]
   public void DontCrash () {
      Console.WriteLine("DontCrash");
   }
   [Test(Ignore = true)]
   public void CrashButIgnored() {
      Console.WriteLine("CrashButIngnored()");
      throw new ApplicationException();
   }
}	
Copyright Patrick Smacchia 2006 2007