code formating

Tuesday, February 14, 2017

.NET get anonimous return expando with added fields

If you happened to be programming in VB.NET and happened to receive an IEnumerable of anonymous objects and if you then need to return the IEnumerable with every element slightly modified, it's a pain.

You have to create a new anonymous object and manually type every property of the old object in every step of the loop. In order to automatise this process, this function comes very handy:

Pass it the anonymous object and a list of key value pairs (these would be the properties to be added) and it returns another object retaining all the old properties and adding the new ones.
Note that the implementation uses the Expando object.

No comments:

Post a Comment

Programatically walk a CATIA tree in .NET

One of the most basic tasks when automating anything in CATIA, it's usually to walk the whole tree and apply some changes to objects in ...