Take class property C#

Asked

Viewed 510 times

1

People need to take all property of a class that is of class type. Ex:

 public class Pessoa{

    public virtual int? Id { get; set; }
    public virtual MinhaClasse1  prop1{ get; set; }
    public virtual MinhaClasse2  prop2{ get; set; }
}

I need to get the property prop1 and prop2. I tried to do something like this below but it didn’t work:

 var propertiess = pessoa.GetProperties().Where(
     prop => prop.PropertyType.BaseType == typeof(object));

Where person would be a Generica entity (Tentity). For the purpose of assembling a ICriterion for generic queries with class relationship.

 foreach (PropertyInfo propriedade in listaPropriedadeClasse) {
            var valorPropriedade = propriedade.GetValue(entity);

            if (!valorPropriedade.IsNull()) {
                criteria.Add(Property.ForName(propriedade.Name).Eq(valorPropriedade));
            }
        }

3 answers

2

An alternative for you would be to have your classes implement an interface, for example IMinhaClasse.

Thus remaining:

public class MinhaClasse1 : IMinhaClasse { }
public class MinhaClasse2 : IMinhaClasse { }
public class MinhaClasse3 { }

Note that the MinhaClasse3 does not implement the IMinhaClasse.

I made a class Person as follows

public class Pessoa
    {

        public virtual int? Id { get; set; }
        public virtual MinhaClasse1 prop1 { get; set; }
        public virtual MinhaClasse2 prop2 { get; set; }

        public virtual MinhaClasse3 prop3 { get; set; }
        public virtual MinhaClasse2 prop4 { get; set; }

        public virtual MinhaClasse3 prop5 { get; set; }
    }

Making the following query Linq to return only the properties that implement the interface IMinhaClasse

var properties = type.GetProperties()
                .Where(property => typeof(IMinhaClasse)
                .IsAssignableFrom(property.PropertyType));

Having as return only to prop1, prop2 and prop4, because the ones with more properties are of the type MinhaClasse3 that does not implement the interface IMinhaClasse

  • I had thought about this alternative too, but in my case I already solved, I marked the comment that solved my problem as an answer. VLW!

  • but in your case you will have problem when you have a Datetime in your class.

  • I did a test here and did not return the fields of type Datetime. But I will be attentive to this question, if I will have to put more conditions in the search, I will adopt your suggestion. Vlw!

0

Type objType = typeof(Pessoa);

        Type[] objTypes = objType.Assembly.GetTypes();
        //LEITURA DE TODAS AS CLASSES
        foreach (Type inType in objTypes)
        {
            objType = inType;
            //LEITURA DAS PROPRIEDADES DA CLASSE
            foreach (object obj in objType.GetProperties())
            {
            }
        }

Follow below the adjustment I made:

Type[] classes = pClass.Assembly.GetTypes();
        foreach (PropertyInfo propriedades in pClass.GetProperties())
        {
            var verificacao = classes.Where(c => c.Name.Contains(propriedades.PropertyType.Name)).FirstOrDefault();
            if (verificacao != null)
            {
            }
        }

Do you need to take the properties of the class that is within your class as well or not? Type, take the properties of the Minhaclasse1 class..

  • It would have to be Generic, or I need to compare class by class. And I don’t know the class that can arrive.

  • Okay, you need to check all of her classes, and all of her properties, correct?

  • Yes @felipedrt.

  • Take a test @Marcosvinicius please.

  • In this code I take all the classes that are within the project, have to catch the classes that are within the class person?

  • But isn’t that what you wanted? If you only want the person class, Voce does so foreach (Propertyinfo properties in objType.Getproperties())

  • but I have to delete the native properties (string, int, bool, etc.) of c# I have to take only the properties that are of the class I created.

  • Got it @Marcosvinicius I’ll set the code here, and I’ll send it to you

  • I got a solution to my problem, I described there in the answers. Vlw by the help!

Show 4 more comments

0


I got a solution:

  var propertiess = entidade.GetProperties().Where(
      prop => prop.PropertyType.IsClass && prop.PropertyType != typeof(string));
  • Beauty, good face!

Browser other questions tagged

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