What is attribute in C#?

Basically, attribute is an information (metadata) that can be attached to your method, class, namespace, assembly etc. Once associated with a program entity, the metadata provided by an attribute can be queried at run time by using a technique called reflection and used in many ways.

Metadata is information about the types defined in a program. All .NET assemblies contain a specified set of metadata that describes the types and type members defined in the assembly. You can add custom attributes to specify any additional information that is required. For example, ObsoleteAttribute causes a compile-time warning to appear, letting the developer know that a method marked with this attribute should no longer be used. More than that, ObsoleteAttribute with error constructor parameter equals true will generate compiler error if marked method will be used in code.

There are predefined attributes in the .NET Framework and its assemblies, for example: [Obsolete], [Serializable], [Conditional] and many others. But you can define you own custom attributes by creating class inherited from Attribute.

public class HelpAttribute : Attribute
{
    public string HelpText { get; set; }
}

This one simply provides HelpText string property, which you can obtain at run time and get some help information for marked code entity. Here is an example of how to read metadata provided by attribute at run time:

public void TestHelpAttribute()
{
    // Get the type object for the class that is using the attribute.
    Type type = typeof(MyClass);

    // Get all custom attributes of type HelpAttribute for the given type.
    object[] attributes = type.GetCustomAttributes(typeof(HelpAttribute), true);

    // Now let's make sure that we got at least one attribute.
    if (attributes != null && attributes.Length > 0)
    {
        // Get the first attribute in the list of custom attributes.
        HelpAttribute attribute = attributes[0] as HelpAttribute;

        // Now we can get the help text for the class "MyClass".
        int helpText = attribute.HelpText;
    }
}

By using the [AttributeUsage] attribute on your custom attributes you can restrict the list of code entities, which might be the target for it. Below our example attribute is now confined only to classes and methods:

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class HelpAttribute : Attribute 
{ 
    public string HelpText { get; set; } 
}

Basically, you can restrict to and apply custom attribute almost to any entity of your code, here is the full list:

  • Assembly
  • Module
  • Class
  • Struct
  • Enum
  • Constructor
  • Method
  • Property
  • Field
  • Event
  • Interface
  • Parameter
  • Delegate
  • ReturnValue
  • GenericParameter
  • All

Short answer

  • Attribute is an information (metadata) that can be attached to your method, class, namespace, assembly etc.
  • Metadata is information about the types defined in a program, it is stored in assembly. Attributes is the way to add additional information about a type to assembly.
  • This metadata can be queried at run time by reflection. Developer can use provided information in any way.
  • There are many predefined attributes in the .NET Framework, but you can create custom attributes by deriving class from Attribute.
  • You can apply custom attributes almost to any entity of your code.

Similar articles

Comments