Back
Close

C# Refresh

Nonsultant
50.5K views

Attributes in C#

Attributes gives us the ability to associating metadata, or declarative information, with code (assemblies, types, methods, properties etc.). This is in C# done using [* and *], if you would like to add an attribute to a property:

[Description("This is a description of my property")]
public string MyProperty { get; set;}

Using reflection is it possible to retrieve the attributes. We are especially going to use this when working with Object-relational mapping (ORM) later in the course.

Example of reading an attribute using reflection:

.NET comes with a range of build in attributes, among these are:

Custom attributes

It's possible to define your own attributes. This done by creating a class which inherits from Attribute,

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io