33

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?

ajma
  • 12,106
  • 12
  • 71
  • 90
  • 4
    Do you mean anonymous objects, instead of, dynamic objects? – Mahesh Velaga Feb 26 '11 at 23:20
  • C#'s type system is very different from JavaScript's. There is no such thing as a "dynamic object"; all objects in C# must be instances of some class. The code you show creates _anonymous objects_, as @Mahesh Velaga said, and the compiler will create a class for each of those objects. You cannot easily get the same effect at runtime if you don't know the properties in advance. As @StackOverflowException said, you'll have to use Reflection.Emit, but that is not an entirely trivial task. – Aasmund Eldhuset Feb 26 '11 at 23:53
  • 5
    @Aasmund: [o rly?](http://msdn.microsoft.com/en-us/library/dd264736.aspx) – BlueRaja - Danny Pflughoeft Apr 13 '11 at 23:51

2 Answers2

32

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.

Luke Foust
  • 2,234
  • 5
  • 29
  • 36
  • 4
    Your code has a bug in it - you should cast item1 and item2 to IDictionary, not Dictionary. SO doesn't want to let me edit because it's less than 6 characters... – Jaco Pretorius May 09 '11 at 10:59
  • 3
    This didn't work for me. I got the following error: Unable to cast object of type '<>f__AnonymousType0`1[System.Int32]' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]. Any suggestions? – Josh Russo Feb 02 '12 at 16:42
  • 6
    This only works if both dynamic items implement an explicit conversion to `IDictionary` - that's true of `ExpandoObject`, but isn't true of anonymous types cast to `dynamic` (which is why it crashes for @JoshRusso) – Keith Feb 08 '12 at 12:02
  • use the following when dealing with dynamic data that isn't a IDictionary. `var convertedData = new RouteValueDictionary(dynamicData);` – FernandoG Mar 21 '21 at 23:14
10

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.

Community
  • 1
  • 1
Yves M.
  • 29,855
  • 23
  • 108
  • 144
  • 1
    This is a helpful answer but I find it bizarre that `GetKeyValueMap` goes and creates a brand new dictionary when it could just return `IEnumerable>` and be implemented in terms of `yield return`. – ta.speot.is Jun 11 '15 at 07:23
  • @ta.speot.is Oh yeah good point.. When I have time I'll update that (I don't have a proper C# environment yet). Feel free to edit if you can test the code.. :) – Yves M. Jun 11 '15 at 08:11
  • 12
    It should be `if (map != null)`, otherwise it won't work with anonymous types. – Mariusz Jamro May 20 '17 at 09:05