Swacblooms🦋

Making the Moves
Menu
  • Home
  • Motivation
  • Education
  • Programming
  • About
  • Contact
  • Privacy Policy
Home
Uncategorized
Creating Attributes and Using Attributes in C#
Uncategorized

Creating Attributes and Using Attributes in C#

Samson Amaugo February 2, 2022

Yello, so in today’s article, I would be writing about Creating Attributes in C# and utilizing them.
You might have seen a code like this:

  public  class  Book
  {
    public  int  BookId  {  get;  set;  }
    [Column("Description")]
    public  string  Title  {  get;  set;  }
  }

And from the code above you might be wondering what [Column(“Description”)] is. And how can the code you are writing use it?
** [Column(“Description”)]** is called an Attribute.
An attribute is used to decorate parts of your code with metadata or information that can be utilized during your code execution.
Attributes in C# can be used on various targets like classes, interface, methods and other types which you can find here. There are some inbuilt attributes in dotnet like:

  • AssemblyTitleAttribute
  • AssemblyDescriptionAttribute
  • AttributeUsageAttribute
  • CallerFilePathAttribute etc

Creating an Attribute

Attributes can be created by inheriting from the Attribute class. The example below shows an Attribute created with the property, MinAge.

[AttributeUsage(AttributeTargets.Property)]
class AgeAttribute : Attribute {
    public int MinAge;
    public AgeAttribute(int minAge = 5){
    MinAge = minAge
    }
}

The AgeAttribute can be called via its alias, Age or its full name AgeAttribute.
The [AttributeUsage(AttributeTargets.Property)] describes that the attribute should only be used for Properties.
To find out other restrictions that can be defined for attribute targets check here.
The AgeAttribute can be added to a property in various ways for instance:

  • [Age] : This would create an attribute by setting the property, MinAge to the default value (5).
  • [Age(20)] : This would create an attribute by setting the property, MinAge to 20.

The class below has the AgeAttribute added to one of its properties:

class Employee {
 public string Name;
 [Age(30)]
 public int Age;
}

But then custom attributes are just bare and have no effect when being used to annotate.
To harness the power of custom attributes, you would need to read the attribute defined for a particular target and implement your logic based on the defined attribute.
For instance, you could prevent your data reader from logging out the age of Employees below the age of 30.

To access the defined attribute in the property of an instantiated object (eg. Employee object), you would need to do something like this:

 Employee person = new(){
    Name = "Sam",
    Age = 20
};
  PropertyInfo propertyInfo = typeof(Employee).GetProperty("Age");
  AgeAttribute  ageAttribute = (AgeAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(AgeAttribute)); 
  if(ageAttribute.MinAge < 30){
// Do something
}
  else {
// Do something else
}

The Attribute.GetCustomAttribute method takes in two arguments in the code above. But it also has other overloads for different cases.
The first argument is of type, PropertyInfo property and the second argument is of type Type
These two arguments allow the GetCustomAttribute method to retrieve the Attribute defined on the Age property. This is possible through what is called Reflection. To know more about reflection check this.

Creating a WeatherLogger using Custom Attributes.

The code below shows an example of creating a Weather logger that decides on which Temperature unit to print to the console based on the attribute attached to the Temperature property. I made use of a weather API. to obtain real-time weather data.
By the time you would be reading this. The API key exposed in the code might not be available.

For the code that uses Microsoft.Extensions.Configuration to retrieve secret key check this.

Thanks for reading through 💪

Prev Article
Next Article

Related Articles

mocking the httpclient
Hello everyone, in today’s post I would be talking about …

Mocking the HttpClient in C#

Ok, we’ve gotten to the final stage and that is …

How to make a PWA and host in Azure-3

About The Author

Samson Amaugo

I am Samson Amaugo. I am a full-stack developer and I specialize in DotNet and the MERN stack.

Leave a Reply

Cancel reply

Search Site

Recent Posts

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#

Categories

  • EDUCATION
  • Motivation
  • Programming
  • Uncategorized

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

RSS Swacblooms

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#
  • Hot Chocolate Data Loader: A Quick Guide
  • Exploring Policy-Based Authorization in Minimal API with .NET 8
  • Using Cloud Firestore in Blazor WebAssembly
  • Serving A VueJs App from DotNet
  • Dynamic Subscriptions in Hot Chocolate 🔥🍫

Swacblooms🦋

Making the Moves
Copyright © 2025 Swacblooms🦋
Swacblooms - Making the Moves