LMB.PropertyExtension 1.0.0

Returns the Name within the System.ComponentModel.DataAnnotations.DisplayAttribute from a property inside a class or from an enumerator and returns the DisplayName within the System.ComponentModel.DisplayNameAttribute from a property inside a class.

Install-Package LMB.PropertyExtension -Version 1.0.0
dotnet add package LMB.PropertyExtension --version 1.0.0
<PackageReference Include="LMB.PropertyExtension" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LMB.PropertyExtension --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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.

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 346 10/3/2017