Creating custom attributes

C# allows you to define your own attributes. This is similar to normal C# programming where you define classes and properties. To define an attribute, the first thing you need to do is to inherit it from the System.Attribute class. The class and properties you define are used to store and retrieve data at runtime.

There are four steps that you need to complete in order to complete defining custom attributes:

  • Attribute usage
  • Declaring attribute class
  • Constructors
  • Properties

Attribute usage can be defined by using System.AttributeUsageAttribute. We already mentioned that there are restrictions on certain attributes, which define where they can be used—for example, in classes, methods, or properties. AttributeUsageAttribte allows us to define such restrictions. AllowMultiple specifies whether this attribute can be used more than once on a specific type. Inherited controls defining child classes form the current attribute class. The following is the general syntax for using the AttributeUsage class:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]

As you might have observed, you can declare the AttributeUsage attribute using its constructor on top of the custom attribute you want to define with the three parameters. With AtributeTargetsAll, you can use CustomAttribute on any type of element that is a class, property, method, and so on. A full list of allowed values is defined at https://docs.microsoft.com/en-us/dotnet/api/system.attributetargets?view=netframework-4.7.2#System_AttributeTargets_All.

Inherited and AllowMultiple are both Boolean properties, which accept true or false.

Once we define AttributeUsage, we can now move on to declare our custom class. This should be a public class and must inherit from the System.Attribute class. 

Now that we have our class declared, we can move on and define our constructors and properties. The framework allows us to define one or more constructors, covering all possible scenarios around a different combination of properties. Let's define a custom attribute. A constructor of these attributes accepts three parameters—AttributeTargets, AllowMultiple, and Inherited:

using System;

namespace Chapter10
{
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, Inherited =false,AllowMultiple = false)]
public class CustomerAttribute : Attribute
{
public CustomerType Type { get; set; }

public CustomerAttribute()
{
Type = CustomerType.Customer;
}
}

public enum CustomerType
{
Customer,
Supplier,
Vendor
}
}

The preceding code defines a custom attribute named CustomerAttribute. We also defined a CustomerType enum that we want to use as an Attribute property. By not defining any parameters in the constructor and assigning the Customer type to a Type property, we are telling runtime, by default, when its value is a customer. Additionally, this attribute is set to be used on either a field or property so that it cannot be used at the class level.

Now, let's examine how we can use this attribute in our class:

namespace Chapter10
{

internal class Account
{
public string CustomerName { get; set; }

[Customer]
public RatingType Rating { get; set; }
}

public enum RatingType
{
Gold =1,
Silver =2,
Bronze=3
}
}

Here, we defined an Account class where we used our custom attribute. We applied an attribute without any parameters. This means that, by default, we create an account of the customer type. In the following section, we will demonstrate how we can retrieve these attributes and use them in our application logic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.20.20