Custom Attributes in .NET

The .NET Framework contains extensibility mechanisms that lets you  create your own custom attributes. In this post we will see how to implement a Custom Attribute and use it in our applications. To start with let’s create a Console application with the name ‘CustomAttributeExample’ as shown below –
clip_image001

Attributes – Allows you to add extra descriptive information into Metadata. You can retrieve this information at runtime.

Now let’s add a class with the name ‘DisqualifyCustomersAttribute’ in our project and write code as shown below –

Custom attributes

Now let’s add a ‘Customers’ class which will use the above attribute to check whether the customer is disqualified or not –

custom attribute

Now let’s write code to add customer information who is disqualified and try to access the information from our custom attribute, as below –

image

The output will be shown as below –

custom attribute .net




No comments: