DynamicObject and WCF support

Posted by rboarman on Stack Overflow See other posts from Stack Overflow or by rboarman
Published on 2010-04-20T00:51:19Z Indexed on 2010/04/20 0:53 UTC
Read the original article Hit count: 917

Filed under:
|
|

Hi,

I was wondering if anyone has had any luck getting a DynamicObject to serialize and work with WCF?

Here’s my little test:

[DataContract]
class MyDynamicObject : DynamicObject
{
    [DataMember]
    private Dictionary<string, object> _attributes = new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {

        string key = binder.Name;

        result = null;

        if (_attributes.ContainsKey(key))

            result = _attributes[key];

        return true;

    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {

        _attributes.Add(binder.Name, value);

        return true;

    }

}


var dy = new MyDynamicObject();
var ser = new DataContractSerializer(typeof(MyDynamicObject));
var mem = new MemoryStream();
ser.WriteObject(mem, dy);

The error I get is:

System.Runtime.Serialization.InvalidDataContractException was unhandled Message=Type 'ElasticTest1.MyDynamicObject' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'System.Dynamic.DynamicObject' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

Any suggestions?

Thanks,

Rick

© Stack Overflow or respective owner

Related posts about c#4.0

Related posts about dlr