How do I set the values of a PropertyInfo of the instantiated classes within a class?

-1

I'm doing a generic method for rounding values of a class (T), but I also have the classes inside that main (T), these classes I can set this way:

IEnumerable<PropertyInfo> childrenProperties = entidade.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(model => model.PropertyType.Namespace == typeof(T).Namespace).ToArray();

I would like to know how to get the values of these classes, with the main class (T) I simply do this:

    foreach (var propertie in decimalPropertiesModel)
    {
        var value = propertie.GetValue(entidade, null);

        if (value != null)
        {
            propertie.SetValue(entidade, decimal.Round((decimal)value, noCasasDecimais), null);
        }
    }

But with the other classes I can not do the same thing.

Remembering that this is a generic method, I have the main class CentroWebOnline, I can set all properties and their values of this class, but within that class I also have the CentroChoose class, I want to know how to set the properties and their values of this class CentroChoose.

    
asked by anonymous 30.07.2015 / 14:05

1 answer

0

If I understand you correctly, you have a class that has a mixture of normal properties (% of%,% with%,% with%, etc.) and properties of classes (of int , double etc.).

If you want to access the properties of all classes, you will need to create a recursive function. I do not exactly have what it would be, but use the following code to inspire you:

public void Recursiva(Type tipo, object entidade) {
    foreach ( prop in entidade.GetType().GetProperties() ) {
       var value = prop.GetValue( entidade, null );
       if ( value is decimal ) {
          //faz algo com decimal
       } else {
          //chama o Recursiva de novo:
          Recursive(value.GetType(), value );
       }
    }
}
    
31.07.2015 / 07:15