我在OnActionExecuted调用时拦截Web API 2管道。在这里,我以递归的方式将动作返回的对象转换为ExpandoObject(即对象本身对象的任何属性也会变为ExpandoObjects,依此类推。)
它XML序列化OK,但只作为字典(大概是因为ExpandoObject实现了IDictionary,它只是从中拉出键和值)。我宁愿看到它序列化,好像它是一个具有属性的对象,而不是一堆键/值对。
如果不创作自己的XML序列化程序,是否可以这样做?
答案 0 :(得分:2)
您可以将ExpandoObject
包装在ISerializable
实施中。它以递归方式包装ExpandoObject
。
[Serializable]
public class SerializableWrapper : ISerializable
{
private IDictionary<string, object> _data;
public IDictionary<string, object> Data
{
get { return _data; }
}
public SerializableWrapper(IDictionary<string, object> data)
{
_data = data;
}
protected SerializableWrapper(SerializationInfo info, StreamingContext context)
{
this._data = new Dictionary<string, object>();
var enumerator = info.GetEnumerator();
while (enumerator.MoveNext())
{
this._data[enumerator.Name] = enumerator.Value;
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (var kvp in this._data)
{
info.AddValue(kvp.Key, Wrap(kvp.Value));
}
}
private static object Wrap(object value)
{
var expando = value as ExpandoObject;
if (expando != null)
{
return new SerializableWrapper(expando);
}
var expandoList = value as IEnumerable<ExpandoObject>;
if (expandoList != null)
{
return expandoList
.Select(Wrap)
.Cast<SerializableWrapper>()
.ToArray();
}
var list = value as IEnumerable;
if (list != null && !(value is string))
{
return list
.Cast<object>()
.Select(Wrap)
.ToArray();
}
return value;
}
}
dynamic obj = new ExpandoObject();
obj.Foo = 3;
obj.Bar = new [] { new ExpandoObject() };
obj.Bar[0].Baz = "Qux";
var wrapped = new SerializableWrapper(obj);
var ser = new DataContractSerializer(typeof(SerializableWrapper), new [] { typeof(SerializableWrapper[]), typeof(object[]) });
var mem = new MemoryStream();
ser.WriteObject(mem, wrapped);
生成:
<SerializableWrapper xmlns="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema">
<Foo i:type="x:int" xmlns="">3</Foo>
<Bar i:type="a:ArrayOfSerializableWrapper" xmlns="" xmlns:a="http://schemas.datacontract.org/2004/07/">
<a:SerializableWrapper>
<Baz i:type="x:string">Qux</Baz>
</a:SerializableWrapper>
</Bar>
</SerializableWrapper>
序列化的XML并不漂亮。您可以使用DataContractResolver
或对XML进行后处理,以减少它的难度。
要再次反序列化,可以使用
mem.Position = 0;
var deserialized = (SerializableWrapper) ser.ReadObject(mem);
另一种方法是实施IXmlSerializable
并改为使用XmlSerializer
。