Search code examples
c#.netappdomainexpandoobjectdynamicobject

Send ExpandoObject to another AppDomain


I'm trying to send one dynamic object (type ExpandoObject) from the main AppDomain to another, and obviously faced the ExpandoObject is not marked as serializable exception.

In addition, this ExpandoObject has inner dictionary member (Dictionary <object, dynamic>) for my needs.

I already tried converting the ExpandoObject into Json using Json.Net (JObject) but the inner dictionaries aren't deserialized as arrays, giving me an exception when trying to access its properties.

Does anyone know how to serialize an dynamic object and sending it to another AppDomain?

Main AppDomain Code:

dynamic dynamicObj = expBuilder.GenerateDynamicObj();
ExpressionInMemory exp = (ExpressionInMemory)ExpressionDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(ExpressionInMemory).FullName);

exp.SetDynamicObj(dynamicObj);

Alternative AppDomain Code:

...
public void SetDynamicObj(dynamic obj)
{
  this._dynamicObj = obj;
}
...

Solution

  • In short, to be able to pass a dynamic object, with the same behaviors of the Expando Object, to another AppDomain, I had to create a new class that inherits Dynamic Object and implements all the ISerializable and IDictionary functions.

    Worked perfectly.