samedi 15 juin 2019

How to create an object list that contain n string properties and one data object but the object type with its properties can change dynamically in C#

I want to load the property "Data" dynamically depend on the object type.

I don't want to do something like this: List listBoxes = new List();

        BaseBoxDTO box = new BaseBoxDTO();
        box.Title = "Title1";
        box.Status = "Status1";

        GeneralInfoBoxDTO GI = new GeneralInfoBoxDTO
        {
            value1 = "string1",
            value2 = "string2"
        };
        box.Data = GI;
        listBoxes.Add(box);

        BaseBoxDTO box2 = new BaseBoxDTO();
        box2.Title = "Title2";
        box2.Status = "Status2";

        TrainingBoxDTO traini = new TrainingBoxDTO
        {
            valorproperty1 = "string1",
            valorproperty2 = "string2"
        };
        box2.Data = traini;
        listBoxes.Add(box2);

Aucun commentaire:

Enregistrer un commentaire