Compiles into a library defining the RamdomSupplier attribute and the RandomMethod attribute : Attribute « Language Basics « C# / C Sharp






Compiles into a library defining the RamdomSupplier attribute and the RandomMethod attribute

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example17_5a compiles into a library defining the RamdomSupplier attribute
  and the RandomMethod attribute
*/

using System;

// declare an attribute named RandomSupplier
[AttributeUsage(AttributeTargets.Class)]
public class RandomSupplier : Attribute
{
  public RandomSupplier()
  {
    // doesn't have to do anything
    // we just use this attribute to mark selected classes
  }
}

// declare an attribute named RandomMethod
[AttributeUsage(AttributeTargets.Method )]
public class RandomMethod : Attribute
{
  public RandomMethod()
  {
    // doesn't have to do anything
    // we just use this attribute to mark selected methods
  }
}
//===================================================
/*
  Example17_5b implements one class to supply random numbers
*/

// flag the class as a random supplier
[RandomSupplier]
public class OriginalRandom
{
  [RandomMethod]
  public int GetRandom()
  {
    return 5;
  }
}

//===================================================
/*
  Example17_5c implements one class to supply random numbers
*/

using System;

// flag the class as a random supplier
[RandomSupplier]
public class NewRandom
{
  [RandomMethod]
  public int ImprovedRandom()
  {
    Random r = new Random();
    return r.Next(1, 100);
  }
}

// this class has nothing to do with random numbers
public class AnotherClass
{
  public int NotRandom()
  {
    return 1;
  }
}
//===================================================
/*
  Example17_5d illustrates runtime type discovery
*/

using System;
using System.Reflection;

class Example17_5d 
{

  public static void Main(string[] args) 
  {

    RandomSupplier rs;
    RandomMethod rm;

    // iterate over all command-line arguments
    foreach(string s in args)
    {
      Assembly a = Assembly.LoadFrom(s);

      // Look through all the types in the assembly
      foreach(Type t in a.GetTypes())
      {
        rs = (RandomSupplier) Attribute.GetCustomAttribute(
         t, typeof(RandomSupplier));
        if(rs != null)
        {
          Console.WriteLine("Found RandomSupplier class {0} in {1}",
           t, s);
          foreach(MethodInfo m in t.GetMethods())
          {
            rm = (RandomMethod) Attribute.GetCustomAttribute(
             m, typeof(RandomMethod));
            if(rm != null)
            {
              Console.WriteLine("Found RandomMethod method {0}"
               , m.Name );
            }
          }        
        }
      }
    }

  }

}

           
       








Related examples in the same category

1.Subclass System.Attribute
2.A simple attribute exampleA simple attribute example
3.Use a named attribute parameterUse a named attribute parameter
4.Creating and using a class attribute.
5.Attribute in class inheritance
6.Defining New Attribute Classes
7.Use AttributeUsage
8.Use a property as a named attribute parameterUse a property as a named attribute parameter
9.Demonstrate the Conditional attributeDemonstrate the Conditional attribute
10.Define contant and use it in Conditional attribute
11.Demonstrate the Obsolete attributeDemonstrate the Obsolete attribute
12.Illustrates use of the Obsolete attribute
13.Shows the use of assembly attributes
14.How to create a custom attributeHow to create a custom attribute
15.Illustrates use of the Conditional attributeIllustrates use of the Conditional attribute
16.Illustrates the GetCustomAttributes methodIllustrates the GetCustomAttributes method
17.demonstrates the flags attribute of an enumeration