C# Custom Attributes

In this chapter you will learn:

  1. How to create your own attribute
  2. Retrieving a Specific Attribute and Checking Its Initialization
  3. A custom attribute based on bool value

Example


using System;//from w w w  . java 2  s . c  o m
using System.Collections.Generic;
using System.Text;

[AttributeUsage(AttributeTargets.Class |
                AttributeTargets.Constructor |
                AttributeTargets.Field |
                AttributeTargets.Method |
                AttributeTargets.Property,
                AllowMultiple = true)]
public class BugFixAttribute : System.Attribute
{
    public int      BugID;
    public string   Date;
    public string   Programmer;
    public string   Comment;

    public BugFixAttribute(int bugID,string programmer,string date){
        this.BugID = bugID;
        this.Programmer = programmer;
        this.Date = date;
    }
}
[BugFixAttribute(1, "B", "01/04/05",Comment = "value")]
public class MyMath
{
    public double DoFunc1(double param1)
    {
        return param1 + DoFunc2(param1);
    }

    public double DoFunc2(double param1)
    {
        return param1 / 3;
    }
    public void Main(){}
}

The code above generates the following result.

Example 2

Retrieving a Specific Attribute and Checking Its Initialization


using System;/*  w  w  w .ja  va  2s  .  c om*/
using System.Reflection;
public class CommandLineSwitchAliasAttribute : Attribute
{
    public CommandLineSwitchAliasAttribute(string alias)
    {
        Alias = alias;
    }
    public string Alias
    {
        get { return _Alias; }
        set { _Alias = value; }
    }
    private string _Alias;
}
class CommandLineInfo
{
    [CommandLineSwitchAliasAttribute("?")]
    public bool Help
    {
        get { return _Help; }
        set { _Help = value; }
    }
    private bool _Help;

}


class MainClass
{
  static void Main()
  {
      PropertyInfo property = typeof(CommandLineInfo).GetProperty("Help");
      CommandLineSwitchAliasAttribute attribute = 
        (CommandLineSwitchAliasAttribute)property.GetCustomAttributes(typeof(CommandLineSwitchAliasAttribute), false)[0];
      if (attribute.Alias == "?")
      {
          Console.WriteLine("Help(?)");
      };
  }
}

The code above generates the following result.

Example 3

A custom attribute based on bool value


using System;/*  w  w  w  . j  a  v  a 2 s .c  o m*/

public class TrueFalseAttribute : Attribute
{
  bool bWritten;

  public bool Written()
  {
    return bWritten;
  }

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

[TrueFalseAttribute(true)]
public class Class1
{
}

[TrueFalseAttribute(false)]
public class Class2
{
}

class MainClass
{
  public static void Main() 
  {
    TrueFalseAttribute u;
    Console.Write("Class1 TrueFalseAttribute attribute: ");
    u = (TrueFalseAttribute) Attribute.GetCustomAttribute(typeof(Class1), typeof(TrueFalseAttribute));
    Console.WriteLine(u.Written());
    Console.Write("Class2 TrueFalseAttribute attribute: ");
    u = (TrueFalseAttribute) Attribute.GetCustomAttribute(typeof(Class2), typeof(TrueFalseAttribute));
    Console.WriteLine(u.Written());
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Attribute Positional vs. Named Parameters
  2. Use a named attribute parameter
Home »
  C# Tutorial »
    C# Types »
      C# Attribute
C# Attributes
C# Obsolete Attribute
C# Conditional Attribute
C# Custom Attributes
C# Attribute parameter
C# Attribute Usage
C# Attribute reflection