samedi 4 janvier 2020

Custom Extension Method on Object

I want to create a custom extension that will work on the properties of an object T, regardless of the type of the property. I need the extension to get the value of the DefaultValue attribute.

Looking at the class below, I want to be able to do something like this:

Employee employee = new Employee();
string defaultNationality = employee.employeeNationality.GetDefaultValue();

Where Employee is defined as

public class Employee
{
    [Browsable(false)]
    public int employeeKey { get; set; }

    [DisplayName("Name")]
    [Category("Design")]
    [Description("The name of the employee.")]
    public string employeeName { get; set; }

    [DisplayName("Active")]
    [Category("Settings")]
    [Description("Indicates whether the employee is in active service.")]
    [DefaultValue(true)]
    public bool employeeIsActive { get; set; }

    [DisplayName("Nationality")]
    [Category("Settings")]
    [Description("The nationality of the employee.")]
    [DefaultValue("Dutch")]
    public string employeeNationality { get; set; }
}




Aucun commentaire:

Enregistrer un commentaire