Get C # class property

1

I need to get all the properties of a class that is of type class. 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 prop1 and prop2 properties. I tried to do something like this below but it did not work:

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

Where person would be a generic entity (TEntity). For the purpose of assembling a ICriterion for generic queries with relationship between classes.

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

            if (!valorPropriedade.IsNull()) {
                criteria.Add(Property.ForName(propriedade.Name).Eq(valorPropriedade));
            }
        }
    
asked by anonymous 29.07.2016 / 16:33

3 answers

0

I have found a solution:

  var propertiess = entidade.GetProperties().Where(
      prop => prop.PropertyType.IsClass && prop.PropertyType != typeof(string));
    
29.07.2016 / 19:52
2

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

Looking like this:

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

Note that MinhaClasse3 does not implement IMinhaClasse .

I made a Person class 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; }
    }

By doing 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));

With return only prop1 , prop2 and prop4 , since those with more properties are of type MinhaClasse3 that does not implement interface IMinhaClasse

    
30.07.2016 / 22:55
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())
            {
            }
        }

Here is 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 inside your class as well or not? Type, get the properties of the class MyClass1 ..

    
29.07.2016 / 16:42