How to create a custom attribute


   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example17_3.cs shows how to create a custom attribute
*/

using System;


public class Example17_3 
{

    public static void Main() 
    {

        UnitTest u;

        // retrieve and display the UnitTest attributes of the classes
        Console.Write("Class1 UnitTest attribute: ");
        u = (UnitTest) Attribute.GetCustomAttribute(
            typeof(Class1), typeof(UnitTest));
        Console.WriteLine(u.Written());
        Console.Write("Class2 UnitTest attribute: ");
        u = (UnitTest) Attribute.GetCustomAttribute(
            typeof(Class2), typeof(UnitTest));
        Console.WriteLine(u.Written());

    }

}


// declare an attribute named UnitTest
// UnitTest.Written is either true or false
public class UnitTest : Attribute
{
    bool bWritten;

    public bool Written()
    {
        return bWritten;
    }

    public UnitTest(bool Written)
    {
        bWritten = Written;
    }
}

// apply the UnitTest attribute to two classes
[UnitTest(true)]
public class Class1
{
}

[UnitTest(false)]
public class Class2
{
}




           
          


Shows the use of assembly attributes

   

/*
shows the use of assembly attributes
*/

using System;
using System.Reflection;
using System.Windows.Forms;

[assembly:AssemblyVersionAttribute("1.0.0.0")]
[assembly:AssemblyTitleAttribute("Example 16.1")]

public class Example16_1 
{
    string privateString;

    public string inString 
    {
        get 
        {
            return privateString;
        }
        set
        {
            privateString = inString;
        }
    }

    public void upper(out string upperString)
    {
        upperString = privateString.ToUpper();
    }

    public static void Main() 
    {
    }
}
           
          


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 );
            }
          }        
        }
      }
    }

  }

}

           
          


Illustrates use of the Obsolete attribute

   


/*
illustrates use of the Obsolete attribute
*/

using System;

public class Example17_1 
{
  // warn the user that Method1 is obsolete
  [Obsolete("Method1 has been replaced by NewMethod1", false)]
  public static int Method1()
  {
    return 1;
  }

  // throw an error if the user tries to use Method2
  [Obsolete("Method2 has been replaced by NewMethod2", true)]
  public static int Method2()
  {
    return 2;
  }

  public static void Main() 
  {
    Console.WriteLine(Method1());
    Console.WriteLine(Method2());
  }
}




           
          


Demonstrate the Obsolete attribute


   


// Demonstrate the Obsolete attribute. 
 
using System; 
 
public class TestAno1 { 
 
  [Obsolete("Use myMeth2, instead.")]  
  static int myMeth(int a, int b) { 
    return a / b; 
  } 
 
  // Improved version of myMeth. 
  static int myMeth2(int a, int b) { 
    return b == 0 ? 0 : a /b; 
  } 
 
  public static void Main() { 
    Console.WriteLine("4 / 3 is " + TestAno1.myMeth(4, 3)); 
 
    Console.WriteLine("4 / 3 is " + TestAno1.myMeth2(4, 3));  
  } 
}