简体   繁体   English

如何在 C# 中从外部 dll 保存对象状态

[英]How to save an object state from an external dll in c#

I know about XML serialization and it's how i save any object to a file when i need to do so, but i need first to access object's class code to add the xml serialization code.我知道 XML 序列化,这是我在需要时将任何对象保存到文件的方式,但我首先需要访问对象的类代码以添加 xml 序列化代码。

What if the class is from an external dll and contains lots of objects contained in the dll, so i can't access internal code to add xml serialization tags to the class, how to save its state to a file when i don't have access to its source code?如果该类来自外部 dll 并且包含 dll 中包含的大量对象,那么我无法访问内部代码以将 xml 序列化标签添加到该类中,如何在我没有时将其状态保存到文件中访问其源代码?

Unless each object has the [Serializable] attribute then you would need to make your own wrapper to go through each class and save everything you need to a file and then when you need it loading loop through the file and set all the values back to how they were.除非每个对象都具有 [Serializable] 属性,否则您需要制作自己的包装器来遍历每个类并将所需的所有内容保存到文件中,然后在需要时加载循环遍历文件并将所有值设置回如何他们是。

If your lucky enough that everything you need to save is Serializable then you can do it in just a few lines of code.如果您足够幸运,您需要保存的所有内容都是可序列化的,那么您只需几行代码即可完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM