可以将动态对象转换为ExpandoObject(c#)

我从驱动程序api(在dll中)获得了一个类型为“密封类”的动态对象。 我想用一些额外的属性来装饰这个对象。

我想做一些事情的效果:

public void expandIT(dynamic sealedObject) { ExpandoObject expand = new ExpandoObject(sealedObject); expand.time = DateTime.Now(); etc.... } 

UPDATE

我喜欢JCL的解决方案。 但是对于我想要做的事情,创建一个ExpandoObject然后将Dynamic sealed class对象作为子属性嵌入,然后将我的属性添加到父ExpandoObject更容易。 谢谢JCL,我正在大脑中冻结如何做到这一点。 一世

不。 dynamic对象在编译时不强制执行该类型,但它不会神奇地使您的对象可扩展(除非它是ExpandoObject )。

但是,您可以使用DynamicObject创建某种包装器或代理…类似于:

 public class ExpandedObjectFromApi : DynamicObject { private Dictionary _customProperties = new Dictionary(); 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; } } 

然后你可以使用它:

 dynamic myExpandedObject = new ExpandedObjectFromApi(sealedObject); 

这应该返回原始对象属性(如果找到),或者如果该名称的属性不在原始对象中,它将把它添加为“自定义”属性。

我在Stack Overflow编辑器中创建了代码并且可能犯了很多错误,它不适合复制/粘贴,并且需要大量的错误检查(如果收到的对象有它们,还需要实现字段和方法) 。 只是写了它,所以你得到了基本的想法。

您还可能想要添加一个特殊属性(例如,称为WrappedObject )并在TryGetMember捕获它,这样您就可以返回原始对象。