我可以在 .NET 4 中序列化 ExpandoObject 吗?
我正在尝试使用 System.Dynamic.ExpandoObject 以便我可以在运行时动态创建属性。后来,我需要传递这个对象的实例,并且使用的机制需要序列化。
当然,当我尝试序列化动态对象时,我得到了异常:
System.Runtime.Serialization.SerializationException 未处理。
程序集“System.Core,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”中的类型“System.Dynamic.ExpandoObject”未标记为可序列化。
我可以序列化 ExpandoObject 吗?是否有另一种方法来创建可序列化的动态对象?也许使用 DynamicObject 包装器?
我创建了一个非常简单的 Windows 窗体示例来重复该错误:
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Dynamic;
namespace DynamicTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dynamic dynamicContext = new ExpandoObject();
dynamicContext.Greeting = "Hello";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, dynamicContext);
stream.Close();
}
}
}
I'm trying to use a System.Dynamic.ExpandoObject
so I can dynamically create properties at runtime. Later, I need to pass an instance of this object and the mechanism used requires serialization.
Of course, when I attempt to serialize my dynamic object, I get the exception:
System.Runtime.Serialization.SerializationException was unhandled.
Type 'System.Dynamic.ExpandoObject' in Assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
Can I serialize the ExpandoObject? Is there another approach to creating a dynamic object that is serializable? Perhaps using a DynamicObject wrapper?
I've created a very simple Windows Forms example to duplicate the error:
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Dynamic;
namespace DynamicTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dynamic dynamicContext = new ExpandoObject();
dynamicContext.Greeting = "Hello";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, dynamicContext);
stream.Close();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法序列化 ExpandoObject,但可以手动序列化 DynamicObject。因此,使用 DynamicObject 的 TryGetMember/TrySetMember 方法并实现 ISerializable,我可以解决真正序列化动态对象的问题。
我在简单的测试应用程序中实现了以下内容:
为什么SerializationInfo 没有 TryGetValue 方法吗? 缺少一块拼图以保持简单。
I can't serialize ExpandoObject, but I can manually serialize DynamicObject. So using the TryGetMember/TrySetMember methods of DynamicObject and implementing ISerializable, I can solve my problem which was really to serialize a dynamic object.
I've implemented the following in my simple test app:
and Why does SerializationInfo not have TryGetValue methods? had the missing puzzle piece to keep it simple.
ExpandoObject
实现了IDictionary
,例如:您可以将字典的内容写入文件,然后通过反序列化创建一个新的 ExpandoObject,将其转换回字典并将属性写回?
ExpandoObject
implementsIDictionary<string, object>
, e.g.:You could write the contents of the dictionary to a file, and then create a new ExpandoObject through deserialisation, cast it back to a dictionary and write the properties back in?
也许回答有点晚了,但我使用 jsonFx 来序列化和反序列化 ExpandoObjects 并且它工作得很好:
序列化:
反序列化
Maybe a bit late to answer but I use jsonFx to serialize and deserialize expandoObjects and it works very well :
serialization:
deserialization