Get properties of a class with condition. C # [closed]

4

I would like to get the properties of my Parent object, but would not like to get the property (States) from my Country class. I tried the way it is in the "Test" class and I could not because I did not know the Type that is the IEnumerable.

I would have some other way of not only bringing this property to a generic condition, because I can not put the name of the property, because this is a process that will be used for all my entity classes, and I need to ignore all properties that are of type IEnumerable.

public partial class Teste
{
   Pais pais = new Pais();
   PropertyInfo[] properties = pais .GetType().GetProperties().Select(x => 
   x.PropertyType != typeof(IEnumerable<T>));
}    

public partial class Pais : EntityBase
{
    public Pais() : base() 
    {
        this.Estados = new HashSet<Estado>();
    }

    public override string TableName { get { return "PAIS"; } }
    public override int Handle { get; set; }
    public string Nome { get; set; }
    public string Sigla { get; set; }
    public virtual ICollection<Estado> Estados { get; set; }
    public override DateTime DataCadastro { get; set; }
    public override string UsuarioCadastro { get; set; }
    public override DateTime? DataAlteracao { get; set; }
    public override string UsuarioAlteracao { get; set; }
}
    
asked by anonymous 08.06.2017 / 22:40

1 answer

3

You need to verify that the property implements the base interface IEnumerable

pais
.GetType()
.GetProperties()
.Where(x => x.PropertyType == typeof (string) || 
       !typeof (System.Collections.IEnumerable).IsAssignableFrom(x.PropertyType))
.ToArray();

IEnumerable is the most basic interface it has, that is, any collection, list, and arrays implement it and consequently the string also implements, since the string is a string, that is, an array of char

In the code I get all properties that are not string or do not implement the IEnumerable interface.

    
08.06.2017 / 23:49