Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to merge C# anonymous objects

Let's say I have two anonymous objects like this:

var objA = new { test = "test", blah = "blah" }; var objB = new { foo = "foo", bar = "bar" }; 

I want to combine them to get:

new { test = "test", blah = "blah", foo = "foo", bar = "bar" }; 

I won't know what the properties are for both objA and objB at compile time. I want this to be like jquery's extend method.

Anybody know of a library or a .net framework class that can help me do this?

like image 951
ajma Avatar asked Feb 26 '11 23:02

ajma


People also ask

How do I merge spaces in C drive?

Right click the C drive at the main interface, then, select "Merge Partitions" at the pop out menu. Step2. The, it will move to the window where you can assign unallocated space to C drive by selecting the partitions in the box. Click "OK".


2 Answers

If you truly do mean dynamic in the C# 4.0 sense, then you can do something like:

static dynamic Combine(dynamic item1, dynamic item2) {     var dictionary1 = (IDictionary<string, object>)item1;     var dictionary2 = (IDictionary<string, object>)item2;     var result = new ExpandoObject();     var d = result as IDictionary<string, object>; //work with the Expando as a Dictionary      foreach (var pair in dictionary1.Concat(dictionary2))     {         d[pair.Key] = pair.Value;     }      return result; } 

You could even write a version using reflection which takes two objects (not dynamic) and returns a dynamic.

like image 127
Luke Foust Avatar answered Sep 19 '22 19:09

Luke Foust


Using TypeDescriptor

As mentioned in comments, Luke Foust solution does not work with anonymous types. The cast to Dictionary does not work, I propose to construct the Dictionaries using the TypeDescriptor.GetProperties method:

public static dynamic CombineDynamics(object object1, object object2) {   IDictionary<string, object> dictionary1 = GetKeyValueMap(object1);   IDictionary<string, object> dictionary2 = GetKeyValueMap(object2);    var result = new ExpandoObject();    var d = result as IDictionary<string, object>;   foreach (var pair in dictionary1.Concat(dictionary2))   {     d[pair.Key] = pair.Value;   }    return result; }  private static IDictionary<string, object> GetKeyValueMap(object values) {   if (values == null)   {     return new Dictionary<string, object>();   }    var map = values as IDictionary<string, object>;   if (map == null)   {     return map;   }    map = new Dictionary<string, object>();   foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))   {     map.Add(descriptor.Name, descriptor.GetValue(values));   }    return map; } 

It's now working with anonymous types:

var a = new {foo = "foo"}; var b = new {bar = "bar"};  var c = CombineDynamics(a, b);  string foobar = c.foo + c.bar; 

Notes:

My GetKeyValueMap method is based on the RouteValueDictionary class (System.Web.Routing). I've rewritten it (using ILSpy to disassemble it) because I think that a System.Web class has nothing to do with object merging.

like image 27
Yves M. Avatar answered Sep 18 '22 19:09

Yves M.