mardi 7 mars 2017

Retrieve property value after typeconverter has been applied

Having a simple YesNoTypeConverter that converts a bool to "Yes" and "No". How can I retrieve the value of a property using reflection with the TypeConverter 'applied'?

e.g. having a class Person:

class Person
{
  [TypeConverter(typeof(YesNoTypeConverter))]
  public bool HatesFakeNews { get; set; }

  public string FirstName { get; set; }
  public string LastName { get; set; }
}

and this code:

Person person = new Person() { FirstName = "Half", LastName = "Abrain", HatesFakeNews = true };

PropertyInfo propertyInfo = person.GetType().GetProperty(nameof(Person.HatesFakeNews));
Debug.WriteLine($"{propertyInfo.GetValue(person)}");

How do get this to show "Yes" instead of "true"? Is there an easier way than retrieving the CustomAttributes and applying the typeconverter myself?





Aucun commentaire:

Enregistrer un commentaire