13

I am getting an dynamic object of type "Sealed Class" from a driver api (in dll). I want to decorate this object with a few additional properties.

I would like to do something to the effect of:

public void expandIT(dynamic sealedObject) {

    ExpandoObject expand = new ExpandoObject(sealedObject);
    expand.time = DateTime.Now();
    etc....
}

UPDATE

I like JCL's solution. But for what I wanted to do, it was easier to create a ExpandoObject and then embed the Dynamic sealed class object as a child property, and then add my properties to the parent ExpandoObject. Thanks JCL, I was in brain-freeze as to how to do this. I

Dr.YSG
  • 7,171
  • 22
  • 81
  • 139

2 Answers2

17

Nope. A dynamic object doesn't enforce the type at compile time, but it doesn't magically make your object expandable (unless it's an ExpandoObject).

You can however, make some kind of wrapper or proxy using DynamicObject... something like:

public class ExpandedObjectFromApi : DynamicObject
{
    private Dictionary<string, object> _customProperties = new Dictionary<string, object>();
    private object _currentObject;

    public ExpandedObjectFromApi(dynamic sealedObject)
    {
      _currentObject = sealedObject;
    }

    private PropertyInfo GetPropertyInfo(string propertyName) 
    { 
       return _currentObject.GetType().GetProperty(propertyName);
    } 

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
      var prop = GetPropertyInfo(binder.Name);
      if(prop != null)
      {
         result = prop.GetValue(_currentObject);
         return true;
      }
      result = _customProperties[binder.Name];
      return true;          
    }      

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
      var prop = GetPropertyInfo(binder.Name);
      if(prop != null)
      {
         prop.SetValue(_currentObject, value);
         return true;
      }
      if(_customProperties.ContainsKey(binder.Name))
        _customProperties[binder.Name] = value;
      else
        _customProperties.Add(binder.Name, value);
      return true;          
    }      
}

And then you can use it like:

dynamic myExpandedObject = new ExpandedObjectFromApi(sealedObject);

This should return the original object properties if found, or if no property of that name is in the original object, it'll add it as a "custom" property.

I've made the code in the Stack Overflow editor and probably made a lot of mistakes, it's not suitable for copy/paste, and it needs tons of error checking (also needs to implement fields and methods, if the received object has them). Just wrote it so you get the basic idea.

You also may want to add a special property (something called WrappedObject, for example) and capture it in TryGetMember, so you can get the original object back.

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • If you implement something like this and you need performance, just as a suggestion, I'd make some kind of cache for the `PropertyInfo` returned by `GetPropertyInfo`. Reflection is always a bit slow :-) – Jcl Apr 11 '16 at 20:10
  • It seems that you are only compiling the ExpandoObject out of properties. How about methods? – toughQuestions Mar 20 '20 at 11:12
  • @toughQuestions yes, that's on the note below the code (`(also needs to implement fields and methods, if the received object has them)`), this was some simple code made in the SO editor and not intended to copy&paste, just giving out the idea – Jcl Mar 20 '20 at 11:39
  • Could you provide an example that sets methods by any chance? – toughQuestions Mar 20 '20 at 12:22
  • 1
    @toughQuestions not right now, but it'd be just overriding `TryInvokeMember` ([docs](https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject.tryinvokemember?view=netframework-4.8#System_Dynamic_DynamicObject_TryInvokeMember_System_Dynamic_InvokeMemberBinder_System_Object___System_Object__)) (same as we do with `TryGetMember`), and call `InvokeMember` ([docs](https://learn.microsoft.com/en-us/dotnet/api/system.type.invokemember?view=netframework-4.8)) on `_currentObject.GetType()` – Jcl Mar 20 '20 at 12:27
12
string json = JsonConvert.Serialize(dynamicObject);

ExpandoObject eo = JsonConvert.Deserialize<ExpandoObject>(json);

Worked for me to change a dynamic to an ExpandoObject.

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
ARs
  • 466
  • 1
  • 7
  • 14