C# 在用户会话之间保留对象而不使用数据库
我正在制作一个简单的 Winform,它读取文本文件并进行一些解析以将其放入 C# 对象中,然后由我的应用程序进行操作。
我想解析一次文件。然后离开一周,回来并将所有对象加载回我的应用程序中。
我不想持久化到数据库,因此希望有某种我可以连接的简单对象持久化框架。
I'm making a simple Winforms that reads in a text file and does some parsing to put it into C# objects that are then manipulated by my app.
I would like to parse the file once. Then go away for a week, come back and load all the objects back into my app.
I don't want to persist to database thus was hoping there was some sort of simple object persistance framework I could hook into.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在谈论序列化。
查看
DataContractSerializer
DataContractJsonSerializer
。您只需添加
DataContract
< /a> 和DataMember
< /a> 属性告诉序列化器如何管理序列化。如果您的对象不会更改(新/旧/重命名的属性和可见性)并且您不介意阅读序列化格式,则可以尝试
BinaryFormatter
。框架中有几个较旧的序列化器(
XmlSerializer
、BinaryFormatter
等),但对序列化格式的最佳控制是使用上面的序列化器。You are talking about serialization.
Look into the
DataContractSerializer
for this. If you want a file that isn't XML you can try theDataContractJsonSerializer
.You simply add the
DataContract
andDataMember
attributes to tell the serializer how to manage the serialization.If your objects will not change (new/old/renamed properties and visibility) and you don't care to read the serialization format, you can try the
BinaryFormatter
.There are several older serializers in the frame work (
XmlSerializer
,BinaryFormatter
and more), but the best control over the serialization format will be with the above).您可以使用应用程序设置或用户设置。使用可以在项目属性下找到的向导非常容易。 这里是教程。
You could use application settings or user settings. Its pretty easy with the wizard that can be found under the projects properties. here is the tutorial for it.
您可以将它们序列化为 XML 文件。简单、快速、容易(除非有人破解了文件)。
You could just serialize them to an XML file. Simple, quick and easy (unless someone hacks the file).
存储对象状态的最简单的非数据库方法是序列化。将您的类标记为 Serialized 或 DataContract,然后使用 BinaryFormatter(如果使用 DataContract,则使用 DataContractSerializer)将对象转换为可持久状态。然后可以将其保存到一个文件中,您可以在应用程序启动备份时加载该文件并以类似的方式反序列化。请注意,类的成员(包括子类)必须以相同的方式可序列化,并且您将丢失对非序列化元素(例如指针和委托引用)的任何引用。
The simplest non-DB method of storing object state is serialization. Mark your class as either Serializable or a DataContract, then use a BinaryFormatter (or DataContractSerializer if you used the DataContract) to convert the object into a persistable state. That can then be saved to a file that you can load when the app starts back up and deserialize in a similar fashion. Be aware that the class's members, including child classes, must be serializable in the same way, and that you will lose any references to non-serialized elements such as pointers and delegate references.
您可以按照之前的建议使用序列化。
保存数据的好地方是 Windows 的 LocalData 文件夹 -
You can use serialization as sugested before.
A good place to save the data in is Windows's LocalData folder -
最简单的方法可能是使用
BinaryFormatter
类将对象直接序列化到文件中。您需要确保所有类型都标记为[Serializable]
,但这种方法既快速又简单。以下是任意对象图的“读取”和“写入”方法的简单示例:
其他方法包括 XmlSerialization(
XmlSerializer
类) - 如果您希望序列化对象易于人类读取,则更好或可编辑,或使用较新的DataContractSerializer
。Probably the simplest way to do this would be to serialize the objects directly to a file, using the
BinaryFormatter
class. You'll need to make sure that all your types are marked as[Serializable]
but this approach is fast and easy.Here is a simple example of a "read" and "write" method for an arbitrary object graph:
Other approaches include XmlSerialization (the
XmlSerializer
class) - better if you want to the serialized objects to be human readable or editable, or using the newerDataContractSerializer
.数据库出了什么问题?如果您不想拥有一个完整的引擎及其设置和相关问题,也许您可以看看基于“文件”的数据库...
我的第一个难点是:
...
取决于对象的复杂性坚持下去,不失为一个好的解决办法。
What is wrong with a database ? If you do not want to have a full engine with its setup and associated problems, maybe you can take a look at "file" based databases...
My first toughs are :
...
Depending the complexity of your objects to persist, it can be a good solution.