Get properties of a class with condition. C#

Asked

Viewed 335 times

4

I would like to get the properties of my Parents object, but I would not like to take the property (States) of my Country class. I tried it the way it is in the "Test" class and I couldn’t because I didn’t know the Type that is the Ienumerable.

There would be some other way to not just bring this property by performing a generic condition, because I can’t put the name of the Property, because this is a process that will be used for all my entity class, 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; }
}
  • Okay, but what is the criterion of what should take or not. The question has no way of knowing. It seems that the name Estados is not the criterion, so what is it? What is the expected result?

  • Then I reopen, people are kicking things that you can’t tell if this is it, so each one gives a solution that doesn’t produce the same result except by coincidence.

  • 2

    She did put the guy who wants her exclusion is Ienumerable ...

  • 2

    For me this question is very clear, there is no reason to be marked as pending...

  • For me it is not. It is ambiguous, when this problem is solved we can reopen.

1 answer

3


You need to check if 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, because the string is a string, that is, a array of char

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

  • 3

    That’s right, buddy, it worked, that’s just what I needed.

  • @Nicolabogar To work is different from always to work, it may work in this case, but in comment outside deleted it is written that you want something generic, which in this case may not work. I don’t even know, because the question doesn’t clear that up. My answer would be different from the other 3 posted and maybe it was what you want for all situations you need. We have no way of knowing. To tell the truth depending on what you need there’s an absurdly simpler solution than that, but you can’t tell with what’s in the question.

  • 2

    @bigown, we will collaborate with the site, it is not only your answers that is correct, in programming there are several paths that arrive in the same place if you said it worked, It is right, otherwise and if you had not closed the question without reason, you could put your answer and she (the Nicola) would choose yours if you were right.

  • @bigown, my problem is the following: I have the Parents Class, which in it has a Ienumerable property<State>, in this case I am taking the properties of this class, but I wish to take all that is not Ienumerable<T>, because in other my classes will also have for example a property Ienumerable<City> on

  • @Tiagos I did not say yours is wrong, I said the question does not make clear what is necessary. So let’s collaborate with the site only by responding to something that has no ambiguities. Several paths that give different results one of them is right, others are wrong. It may be that yours is right, but there is no way to know. It may be that this initial test gave the expected result, but it may not solve when doing something other than this. I just want the question to help everyone, here is not a forum.

  • Continuation -> my State class. And this process that I’m taking the properties it’s done in a generic way, so I’d like it to be something more generic, like, don’t take the properties that implement Icollection. I need this because these classes represent tables in my database, and there is this field States in the countries table, this collection was created only for assistance, like an Entityassociation. I don’t know if it’s clear, you can understand ?

  • That’s not how it works, put a lot of kick and one person chooses who was luckier, the site never worked like this.

  • @Nicolabogar Do not use comments to clarify, edit the question to be clear.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.