How to increment an anonymous type object in C #

I often need to enlarge an object with a property, for example. So far (tired of this), and this is also ugly) I did it like this:

var someListOfObjects = ...;

var objectsWithMyProperty = from o in someListOfObjects
                            select new
                            {
                                o.Name,    /* Just copying all the attributes I need */
                                o.Address, /* which may be all of them. */

                                SomeNewProperty = value
                            };

Is there any reasonable way to do this? I have done this before:

var objectsWithMyProperty = from o in someListOfObjects
                            select new
                            {
                                OldObject = o,           /* I access all of the old properties from here */    
                                SomeNewProperty = value
                            };

I think this can be done with some reflection, but I believe that there is a faster approach that does something equivalent to the first cumbersome approach.

Thanks Lasse

+5
source share
2 answers

I think you are looking for something like ExpandoObject that was added in C # 4.0 along with a dynamic type

http://msdn.microsoft.com/en-us/magazine/ff796227.aspx

, / . , :

obj.NewProperty = newValue

ExpandoObject .

, .

+3

. . - , .

+4

All Articles