使用 C# 序列化内存对象
我有一个程序可以从脚本文件中获取一些代码并进行编译。 而且效果很好。
问题是:在脚本中我声明了几个类,并且我想序列化它们。 显然,C# 序列化程序(xml 和二进制)不喜欢序列化和反序列化内存中程序集中定义的对象。
我宁愿不离开内存中的程序集,所以我正在寻找另一种序列化方式,但以防万一,是否可以在内存中构建程序集并最终将其写入文件?
I've got a program that picks up some code from script files and compiles it.
And It works fine.
The problem is: in the scripts I declare a couple of classes and I want to serialize them.
Obviously the C# serializer (xml and binary) doesn't like to serialize and the de-serialize object defined in a in-memory assembly.
I prefer to don't leave the in-memory assembly so i'm looking for another way of serializing, but in case, is possible to build assembly in memory and eventually write it on file ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您始终可以使用反射编写自己的 ToXml 函数,将属性数据写入字符串。 然后你的对象将反序列化自身。
只是一个想法。
You could always write your own ToXml function using reflection to write out your property data to a string. Then your object would deserialize itself.
Just a thought.
如果您想动态创建程序集,请查看通过反射发出的 IL。 这是一篇可以帮助您入门的好文章。
If you want to create assemblies dynamically look into IL emitting via reflection. Here is a good article to get you started.
因此,为了澄清一下,您是否询问如果未应用 [Serialized] 属性,如何序列化类型?
一种解决方案是使用 WCF 数据协定序列化程序: http://msdn.microsoft .com/en-us/library/ms731923.aspx。
显然,只有当您可以针对 .Net 3.0 或更高版本时,这才有效。
或者,您可以实现 ISerializationSurrogate。 Jeffrey Richter 在 http://msdn.microsoft.com/en 上有精彩的介绍-us/magazine/cc188950.aspx。
So just to clarify, are you asking how you can serialize a type if it hasn't got the [Serializable] attribute applied?
One solution is to use the WCF Data Contract Serializer: http://msdn.microsoft.com/en-us/library/ms731923.aspx.
Obviously this will only work if you can target .Net 3.0 or higher.
Alternately you can implement an ISerializationSurrogate. Jeffrey Richter has a great introduction at http://msdn.microsoft.com/en-us/magazine/cc188950.aspx.
我会尽可能避免所有内置序列化,两者都严重损坏。 例如,XML 序列化不支持字典,普通序列化/SOAP 不支持泛型。 两者都有版本控制问题。
这很耗时,但创建 ToXML 和 FromXML 方法可能是最有效的方法。
I would avoid all built-in serialization whenever possible, both are badly broken. For example, XML serialization doesn't support dictionaries and normal serialization/SOAP doesn't support generics. And both have versioning issues.
It is time consuming, but createing ToXML and FromXML methods is probably to most effective way to go.
看看这里自定义序列化程序,是字典 XML 序列化的示例
Hava a look at here for custom serialisers, which is a sample for dictionary XML serializing
我对 XmlSerializer 无法序列化动态生成的类型的说法感到有点困惑。 XmlSerializer 在构造期间也会动态生成它自己的序列化代码,因此它序列化您的类型应该没有问题。
您可能需要使用适当的属性来装饰动态类,具体取决于您生成的内容(如派生类),但在您描述的情况下使用 XmlSerializer 应该不会有任何问题。
如果您可以发布有关 XmlSerializer 给您提供的问题的详细信息,我可以帮助您找出问题所在。
另外,我相信自动生成代码通常是一件好事。 我常常不得不返回一个类来修复一个或所有复制/粘贴/保存/加载功能,只是因为有人在添加新变量时忘记更新它们。 保存/加载代码是样板代码。 让计算机来写吧。
I'm slightly confused by the statement that the XmlSerializer can't serialize dynamically generated types. The XmlSerializer generates it's own serialization code dynamically as well during construction so there should be no issue with it serializing your type.
You may need to decorate your dynamic classes with the appropriate attributes, depending on what you are generating (like derived classes), but there shouldn't be any issue with using the XmlSerializer in the situation you described.
If you could post details about the issues the XmlSerializer is giving you I can help you work out what the problem is.
Also, I'm of the belief that auto-generating code is in general a blessing. All to often have I had to go back into a class to fix one or all of the copy/paste/save/load functions, just because someone forgot to update them when adding a new variable. Save/Load code is boiler plate code. Let the computers write it.