动态对象序列化 [英] Dynamic Object Serialization
问题描述
我试图序列与的BinaryFormatter
A DynamicObject
类,但是:
I tried to serialize a DynamicObject
class with BinaryFormatter
, but:
- 输出文件太大,不完全是电线友好
- 循环引用没有处理(卡住而序列化)
由于序列化 DynamicObject
的意思本身很小,这里的类我试图序列:
Since serializing a DynamicObject
means very little by itself, here's the class I tried to serialize:
[Serializable()]
class Entity
: DynamicObject, ISerializable
{
IDictionary<string, object> values = new Dictionary<string, object>();
public Entity()
{
}
protected Entity(SerializationInfo info, StreamingContext ctx)
{
string fieldName = string.Empty;
object fieldValue = null;
foreach (var field in info)
{
fieldName = field.Name;
fieldValue = field.Value;
if (string.IsNullOrWhiteSpace(fieldName))
continue;
if (fieldValue == null)
continue;
this.values.Add(fieldName, fieldValue);
}
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
this.values.TryGetValue(binder.Name, out result);
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
this.values[binder.Name] = value;
return true;
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (var kvp in this.values)
{
info.AddValue(kvp.Key, kvp.Value);
}
}
}
(我。想我可以用一个ExpandoObject,但那是另一回事)
(I guess I could have used an ExpandoObject, but that's another story.)
下面是一个简单的测试程序:
Here's a simple test program:
static void Main(string[] args)
{
BinaryFormatter binFmt = new BinaryFormatter();
dynamic obj = new Entity();
dynamic subObj = new Entity();
dynamic obj2 = null;
obj.Value = 100;
obj.Dictionary = new Dictionary<string, int>() { { "la la la", 1000 } };
subObj.Value = 200;
subObj.Name = "SubObject";
obj.Child = subObj;
using (var stream = new FileStream("test.txt", FileMode.OpenOrCreate))
{
binFmt.Serialize(stream, obj);
}
using (var stream = new FileStream("test.txt", FileMode.Open))
{
try
{
obj2 = binFmt.Deserialize(stream);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Console.ReadLine();
}
在这里把一些断点有帮我看看OBJ2内容,它看起来像原始数据被正确反序列化,尽管上述的缺点,如果你得到富有想象力的和移动的数据。
Putting some breakpoints here and there helped me have a look at obj2 contents and it looks like the original data is correctly deserialized, though with the above shortcomings if you get imaginative and move data around.
我看了一下马克Gravell的protobuf的-net,但我真的不知道如何在这样一个背景下使用它(我甚至不知道我从仓库中拿起了正确的版本,但嘿)。
I had a look at Marc Gravell's protobuf-net, but I'm not really sure how to use it in such a context (I'm not even sure I picked up the right version from the repository, but hey).
我知道这是话多的代码,但我不认为我能解释的情况更好。请你告诉我们,如果有什么东西我可以补充,使这个问题更加清晰。
I know it's more code than words, but I don't think I can explain the scenario any better. Please do tell if there's something I can add to make this question clearer.
任何帮助深表感谢。
推荐答案
我98%肯定,这个序列将动态对象的工作。
I'm 98% certain that this sequence will work for a dynamic object.
- 转换对象在Expando对象
- 投的expando对象为类型字典
- 使用protobuf网Serializer.Serialize / .Deserialize按正常
- 转换字典为Expando对象
- convert object to an Expando Object
- cast expando object to be of type Dictionary
- use ProtoBuf-net Serializer.Serialize / .Deserialize as per normal
- convert dictionary to Expando Object
您可以将对象转换为名称/值对的集合转流。
You can convert objects to a collection of name/value pairs for transfering.
这是一个什么样的动态能做的只是一小部分,但也许这是足以让你。
That's just a small subset of what dynamic can do, but perhaps it is enough for you.
有一些自定义代码来处理一些上面说的转换,我可以告诉你,如果有兴趣的。
There's some custom code to handle some of the conversions above that I can show you if there's interest.
我没有动态时是一个占位符的解决方案一类。对于这种情况下,我建议得到的类型和使用switch语句序列化/反序列化根据您的需要。对于后一种情况下,你需要放置的东西,以指示哪种类型,你需要通用的反序列化(串/ ID /完全限定的类型名称的/ etc)。假设您正在处理预计类型的列表
I don't have a solution for when dynamic is a placeholder to a class. For this case I'd suggest getting the type and using a switch statement to serialize / deserialize as you require. For this last case, you'd need to place a something to indicate which type of generic deserialization that you need (string / id / fully qualified type name / etc). Assumption is that you are dealing with a list of expected types.
注:为Expando实现IDictionary的。在Expando仅仅只是键/值对的列表。 IE浏览器。你点到的东西是关键,和值是无论从任何函数链中实现了回报。 。有用于自定义语法糖经历一组动态的接口,但大部分时间你不会看他们。
Note: Expando implements IDictionary. An Expando is merely merely a list of key/value pairs. ie. the thing you dot into is the key, and the value is the return from whatever chain of functions implements that. There are a set of dynamic interfaces for customising the syntactic sugar experience, but most of the time you wont to look at them.
裁判:
- 词典从IDictionary中使用构造 - 的 http://msdn.microsoft.com/en-us/library/et0ke8sz(v = vs.110)的.aspx
- 的IDictionary /字典来为Expando - 的http:// theburningmonk .COM / 2011/05 / idictionarystring对象到expandoobject扩展法/
- Dictionary from IDictionary using the constructor -- http://msdn.microsoft.com/en-us/library/et0ke8sz(v=vs.110).aspx
- IDictionary/Dictionary to Expando -- http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/
这篇关于动态对象序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!