mardi 5 avril 2016

How to skip unwanted properties when using reflection in C#

I am using reflection to get all the properties from class like this:

var props = item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); // todo: cache & filter not-needed props)

var itemStr = string.Join(", ", 
                     props.Select(p => p.GetValue(item, null)?.ToString())
                          .ToArray());

item is object type Person defined like this:

public sealed class Person : KnowYourCustomerBase
{
     [DataMember]
     public string Surname { get; set; }

     [DataMember]
     public string FirstName { get; set; }

     [DataMember]
     public string MiddleName { get; set; }

     [DataMember]
     public string Address4 { get; set; }

     [DataMember]
     public string DateOfBirth { get; set; }

     [DataMember]
     public string NationalID { get; set; }

     [DataMember]
     public string Gender { get; set; }
}

How can I edit line of code that's getting all the properties and return them for example all except NationalID?





Aucun commentaire:

Enregistrer un commentaire