LMB.PropertyExtension
1.0.0
dotnet add package LMB.PropertyExtension --version 1.0.0
NuGet\Install-Package LMB.PropertyExtension -Version 1.0.0
<PackageReference Include="LMB.PropertyExtension" Version="1.0.0" />
paket add LMB.PropertyExtension --version 1.0.0
#r "nuget: LMB.PropertyExtension, 1.0.0"
// Install LMB.PropertyExtension as a Cake Addin #addin nuget:?package=LMB.PropertyExtension&version=1.0.0 // Install LMB.PropertyExtension as a Cake Tool #tool nuget:?package=LMB.PropertyExtension&version=1.0.0
Property Extension
This extension contains methods for retrieving the Name
value of an attribute of type System.ComponentModel.DataAnnotations.DisplayAttribute
and the DisplayName
value of an attribute of type System.ComponentModel.DisplayNameAttribute
.
How it works?
This extension is using Reflection to find a property (informed in the method parameter) and from there, retrieve the Name
from an attribute of type System.ComponentModel.DataAnnotations.DisplayAttribute
or the DisplayName
from an attribute of type System.ComponentModel.DisplayNameAttribute
.
How to use this extension?
For this example, we will use the Customer
class and it's CustomerCategory
enumerator.
Note that the both are decorated with Display
or DisplayName
attributes.
To use those attributes, you need to add a reference to the System.ComponentModel.DataAnnotations
assembly.
using System.ComponentModel.DataAnnotations;
namespace MyNamespace
{
public class Customer
{
[Display(Name = "Customer's Id")]
public int Id { get; set; }
[DisplayName("Customer's Full Name")]
public string Name { get; set; }
[Display(Name = "Customer's Category")]
public CustomerCategory Category { get; set; }
}
public enum CustomerCategory
{
[Display(Name = "Silver Category")]
Silver = 1,
[Display(Name = "Gold Category")]
Gold = 2,
[Display(Name = "Platinum Category")]
Platinum = 3
}
}
From a Class:
// Creates a new instance of the Customer class
var customer = new Customer
{
Category = CustomerCategory.Platinum,
Id = 1,
Name = "Homer 'Jay' Simpson"
};
// Get the DisplayName from System.ComponentModel.DisplayNameAttribute
var displayNameAttribute = customer.GetDisplayNameAttributeValue("Name");
// Get the Name from System.ComponentModel.DataAnnotations.DisplayAttribute
var nameAttribute = customer.GetDisplayAttributeValue("Id");
From an Enum:
// Get the Name from System.ComponentModel.DataAnnotations.DisplayAttribute
var nameAttribute = CustomerCategory.Gold.GetDisplayAttributeValue();
Where to get this extension?
You can install this extension direct from Nuget:
Install-Package LMB.PropertyExtension
Or you can download this github project and copy the PropertyExtension.cs
file direct into your project.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net46 is compatible. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,137 | 10/3/2017 |