Dynamically add a nested T class and fill it with another T class (GetProperty, GetType, SetValue)

40 Views Asked by At

I just want to pass a class T as a parameter. Example:

from:

SetValue (Car.parts, "engine");

to:

SetValue (T.partes, "engine");

simply pass your nested class of a generic as a parameter.

Thank you very much for the help.

HERE THE DETAILED. but I just need the top one.

Good day, I would like to dynamically fill a class T with some values. here my example:

This class is familiar.

Class Model model = new ClassModel ();

has a nested class

model.autos = new ClassModel.Auto ();

Write ClassModel = typeof (ClassModel);

var AutoClass = ModelClass.GetProperty ("autos");

var AutoProp = Type.GetType (ClaseAuto.GetMethod.ReturnType.FullName) .GetProperties ();

AutoProp [0] .SetValue (model.autos, 1);

// here this works and passes the parameter, but if it were a class T How would the value for cars pass? example:

AutoProp [0] .SetValue (T.autos, 1);

where cars are unknown because the class is T. and at the same time fill the class T which is easier to identify and fill since the first T corresponds to a class.

ClassModel.SetValue (T, AutoProp);

after this to corroborate the success of the variable is passed to a variable when the process ends

/ * Once my class is recovered, I already know all its methods, but inside class T I do not know the methods, but I send the class cars by name = "autos" and so I get to know it but I still do not know how to pass on the values . * / int result = model.autos.id;

Thank you very much for the help provided.

1

There are 1 best solutions below

0
On

Simply the class T you get the nested class, then if you want more all its values ​​as a normal class add GetValue (T) and ready to pass the class as if it were a normal class.

the class tests the one daughter class of T

SetValue(T.GetType().GetProperty("proof").GetValue(T) , 1);