mercredi 14 février 2018

Convert list of nested object to list of flat object

I have the following nested object:

I would like to convert this object to flat object, like this:

//just example of code for nested object
var someObj = new List<NestedClass1>
                {
                    new NestedClass1
                    {
                        prop1 = "",
                        prop2 = "",
                        prop3 = new List<NestedClass2>
                        {
                            new NestedClass2
                            {
                                prop4 = "",
                                prop5 = new List<NestedClass3>
                                {
                                    new NestedClass3
                                    {
                                        prop6 = "",
                                        prop7 = 0,
                                        prop8 = 0,                                        
                                    },
                                    new NestedClass3
                                    {
                                        prop6 = "",
                                        prop7 = 0,
                                        prop8 = 0,                                        
                                    },
                                }
                            },
                            new NestedClass2
                            {
                                prop4 = "",
                                prop5 = new List<NestedClass3>
                                {
                                    new NestedClass3
                                    {
                                        prop6 = "",
                                        prop7 = 0,
                                        prop8 = 0,                                        
                                    }
                                }
                            }
                        }
                    }
                };

The result object:

var someResultObj = new List<FlatClass>{
    new FlatClass{
        prop1 = "",
        prop2 = "",
        prop4 = "",
        prop6 = "",
        prop7 = 0,
        prop8 = 0,
    }
}

Do you know any ways to do it use reflection or something else?

I wouldn't like use loop and initialize my flat object from this loop and 3d party libs.

I would like to do it flexible, is it possoble?

Thanks.





Aucun commentaire:

Enregistrer un commentaire