DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • JSON-Based Serialized LOB Pattern
  • What Is Pydantic?
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Attribute-Level Governance Using Apache Iceberg Tables

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Data Engineering
  3. Data
  4. C# Attributes Basics

C# Attributes Basics

A simple guide to C# attributes uses. Attributes are a powerful method of associating metadata or declarative information with code.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Jan. 04, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
2.2K Views

Join the DZone community and get the full member experience.

Join For Free

Attributes are a powerful method of associating metadata or declarative information with code.

You can think of attributes as adding tags/notes to code:


Adding tags/notes to code.

There are various uses of attributes, such as:

  • Add meta-data to code elements.
  • Configure conditional compilation.
  • Decorate code as obsolete.
  • Data validation rules/error messages.
  • Control serialization/deserialization.
  • Azure Function bindings.
  • …others

Attributes and Reflection

Attributes and Reflections are two separate but strongly related topics.

Reflection allows you to dynamically find out at runtime what methods and properties etc., are available on your types.

Attributes let you attach additional metadata to your code, and the connection with reflection is that you can use reflection to read that metadata.

Attributes are compiled into the resulting assemblies. Attributes are applied at compile-time, not at runtime.

Next, let's see some code examples. All the example's source code is available on this git repo.

Attribute Basic Example – Built-in Attributes

Let's see a basic example of built-in attributes provided by the framework itself.

You can see the Key attribute is applied to the Id property.


You can see the Key attribute is applied to the Id property.

And here is the reflection code which accesses this attribute:

the reflection code which accesses this attribute.


Don’t worry if this code is unclear to you yet; we will go into details later in the post.

Here is another built-in attribute, Obsolete, applied to the Device class:

An Obsolete attribute is a declarative tag used while declaring a type or a member of a type to indicate that it should no longer be used.

With this attribute applied, if we now try to use this type (Device class) in our program, Visual Studio will give us the following warning:

Visual Studio will give us the following warning.

So, this is another common use of the attribute, mark code obsolete.

So far, we’ve applied attributes to both class and its properties. Later we will see that we can apply attributes to a wide range of targets.

Attribute Example – Friendly Text for Enums

Let’s see an example use of Attributes.

Following is the updated code of the Device Class. We’ve defined the DeviceType Enum and used it in the Device class:

We’ve defined the DeviceType enum and used it in the Device class.


And here is the console output from the program, which outputs a string representation of the DeviceType Enum:

The console output from the program.


Now, let's say that we want human-friendly readable text instead of the string representation of Enum, e.g., “Virtual Device” instead of “VirtualDevice.”

Here is how we can do this via attributes:

  1. First, create an attribute to add human-friendly text to enum items.
  2. Then, use reflection to extract that text to display friendly text.

Create a Custom Attribute

The following code shows how to create a custom attribute for our requirement:

How to create a custom attribute for our requirement.


As you can see, that our custom attribute is just a simple class that inherits from the built-in Attribute type, with one property to hold the friendly text, which is set via the constructor. This follows a naming convention that says you need to suffix your custom attribute with Attribute.

Notice the use of AttributeUsage applied to restrict it to be applied only to fields (enum values are internally implemented as static fields). AttributeUsage is another built-in attribute provided by the .NET framework itself.

Apply Custom Attribute

The following code shows that the FriendlyText custom attribute is applied to enum items:

FriendlyText custom attribute is applied to enum items


Extraction via Reflection

So, we have declared and applied the custom attribute (FriendlyText). This is the first half of the solution.

The other half is to extract that custom attribute via reflection and use it in our code. For example, the following code does this part:

The other half is to extract that custom attribute via reflection and use it in our code.

And we can now use it in our code as needed:
And we can now use it in our code as needed.

Here is the console output of the usage:


Here is the console output of the usage.


Let's see another example of a custom attribute next.

Attribute Example – Attribute on a Type

For this example, we have the following setup:

Vehicles Model


Here is the C# implementation of this domain model.

Here is the C# implementation of this domain model. Most of the code is self-explanatory, here Vehicle class is acting as a base class, and other classes (Car, Truck, FlyingCar) are derived classes with few of their own properties:

Most of the code is self-explanatory, here Vehicle class is acting as a base class, and other classes (Car, Truck, FlyingCar) are derived classes with few of their own properties.

In the main method, we have the following simple usage:


Simple usage.


ReportHelper.cs

Report helper.


Requirement

Here, in our domain, we might have vehicles that are just for testing/prototype purposes, and those should not be used in our application as actual vehicles. To facilitates this requirement, we can mark those types which can be treated as actual vehicles(e.g., can be included in reports, can be sold, etc.).

We can achieve this by creating a custom attribute as follows:


Now, we can mark desired code with this attribute as shown below:

 Mark desired code with this attribute.


At this point, the attribute is defined and applied.

Next, we need to test whether a given type has that attribute applied, and this is where reflection comes in:

Test whether a given type has that attribute applied.


We can now simply use this helper method as shown below:

Use Report Helper.

This is also a very common use of attributes to mark your types for certain conditions.

Attribute Example – Attribute on a Type

Here is another example of applying an attribute to a type:

Target Class to Apply Attribute

Target Class to Apply Attribute.

Custom Attribute

Following is a simple company attribute to apply to Server.

Simple company attribute to apply to Server.

Apply Custom Attribute


Apply Custom Attribute.


Usage Example

Usage Example.


Note: server.GetType and typeof(Sever) result same. typeof() is used when you know the type upfront.

Attribute Example – Attribute on a Type and Properties

Now, for this example, I created a custom attribute (InfoAttribute), which can be applied to both a Type (e.g., Server) or type one or more Properties. This attribute will hold some help/info text.

The following are the components:

InfoAttribute

InfoAttribute


Here is the attribute applied to type and a few of its properties:


Attribute applied to type and a few of its properties.

And here are the calling code and output:

 Calling code and output.


You can check the source code available from this git repository for more details.

ActionFilter Attribute

I will not go into details, but ActionFilters are very common to use in .NET API development.

An Action filter is an attribute that you can apply to a controller action — or an entire controller — that modifies the way in which the action is executed.

Summary

C# Attributes are a very powerful and useful feature. They allow us to add meta-data to our code, and we can use Reflection to read that metadata. Furthermore, attributes are applied at compile-time.

Attributes can be applied to a wide variety of code elements such as classes, fields, attributes, properties, methods, etc. To see a list of common targets and more information about C# attributes, you can visit the official documentation on this link.

Let me know if you have some comments or questions. Till next time, Happy coding.

Attribute (computing) Data (computing) code style csharp Data Types

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JSON-Based Serialized LOB Pattern
  • What Is Pydantic?
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Attribute-Level Governance Using Apache Iceberg Tables

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

OSZAR »