简体   繁体   English

如何保存C#app的状态以便以后加载

[英]How to save the state of an C# app to be able to load later

I have a program in which the user adds multiple objects to a scene. 我有一个程序,用户在其中向场景添加多个对象。 These objects are all represented as a classes. 这些对象都表示为类。 Now I want the user to be able to save the scenario, so it can be loaded again. 现在我希望用户能够保存场景,因此可以再次加载。

Is there some genereic save method I can use, or do I have to write my own that saves the values of all properties and such and then create new objects and insert the saved values into them when the user loads the scenario again. 是否有我可以使用的基因保存方法,或者我必须编写自己的保存所有属性等的值,然后创建新对象并在用户再次加载方案时将保存的值插入其中。

/P / P

Edit: Thanks for the quick replys guys. 编辑:谢谢你的快速回复。 I'm actually storing default values for the objects in a SQL compact 3.5 database right now. 我实际上是在SQL Compact 3.5数据库中存储对象的默认值。 So if anyone knows how to serialize to SQL, that would be great info. 因此,如果有人知道如何序列化为SQL,那将是很好的信息。

Edit 2: Thanks again! 编辑2:再次感谢! I'm going to serialize to xml, then store it as a string to the SQL database. 我将序列化为xml,然后将其作为字符串存储到SQL数据库中。

Assuming your objects have all values available as public properties, you can use an XMLSerializer to convert the object to an XML string, then use a XMLDeserializer to re-create the object. 假设您的对象具有可用作公共属性的所有值,您可以使用XMLSerializer将对象转换为XML字符串,然后使用XMLDeserializer重新创建对象。

There is an extension method to accomplish this here . 有一个扩展方法来做到这一点在这里

If you have private variables, you can still write your own custom serialize/deserialize methods. 如果您有私有变量,您仍然可以编写自己的自定义序列化/反序列化方法。

Edit in response to original question edit: 编辑以回复原始问题编辑:

You can use any of these serialization techniques to store the string into a database. 您可以使用任何这些序列化技术将字符串存储到数据库中。 Another option would be to use an ORM (Object Relational Mapper) to bridge the gap between your objects and the databases. 另一种选择是使用ORM(对象关系映射器)来弥合对象和数据库之间的差距。

Usually one would use a database to do this kind of thing. 通常会使用数据库来做这种事情。 That's probably the recommended practise. 这可能是推荐的做法。

However if you want a quick and dirty method you can serialise to Xml or Binary with relatively little code. 但是,如果你想要一个快速而又脏的方法,你可以使用相对较少的代码序列化为Xml或Binary。 Check out the System.Runtime.Serialization stuff. 查看System.Runtime.Serialization的内容。 The trade-off of Xml or Binary is that versioning sucks big time. Xml或Binary的权衡是版本化时间很长。

Consider a collection of xml files containing objects from your 1.0 app. 考虑包含1.0应用程序中对象的xml文件集合。 If you've added fields to those objects since 1.0 then they wont be compatible with any new 1.1 objects. 如果从1.0开始为这些对象添加了字段,那么它们将不兼容任何新的1.1对象。 To get around this you have to write a manual update sequence from type 1.0 -> 1.1. 要解决此问题,您必须编写1.0 - > 1.1类型的手动更新序列。 Even worse if you use autodiscovery code (which automatically serialises) such as they xml or binary serializer you may even have to name your objects different names Product1 / Product1.1 / etc, it's either that or write your own serialisation code. 更糟糕的是,如果您使用自动发现代码(自动序列化),例如它们的xml或二进制序列化程序,您甚至可能必须将对象命名为不同的名称Product1 / Product1.1 / etc,它可以是或者编写您自己的序列化代码。

If your app starts approaching any level of seriousness I'd say you'll probably want to use a database. 如果您的应用程序开始接近任何严重程度,我会说您可能想要使用数据库。 SqlLite is very nice and simple for 1 user apps, anything bigger and you'll want a proper RDBMS. 对于1个用户应用程序,SqlLite非常简单和简单,任何更大的东西,你都需要一个合适的RDBMS。

You'll need to look at .NET Serialization, which is a big topic. 您需要查看.NET序列化,这是一个很大的主题。 Here's an introductory tutorial. 这是一个入门教程。

您还可以查看db4o ,它是一个简单的对象数据库。

.NET Serialization is the simplest way to do this, although versioning can be a problem as was previously mentioned. .NET序列化是最简单的方法,尽管版本控制可能是一个问题,如前所述。 This may not be a big problem depending on how complicated your objects are. 这可能不是一个大问题,具体取决于您的对象有多复杂。 Your serialization/deserialization logic needs to be smart enough to handle cases where attributes are missing, or have default behavior for new attributes that were not present in serialized objects that were serialized by older versions. 序列化/反序列化逻辑需要足够智能,以处理缺少属性的情况,或者具有旧版本序列化对象中不存在的新属性的默认行为。 Mainly this will be handled by catching SerializationExceptions and dealing with them there. 主要是通过捕获SerializationExceptions并在那里处理它们来处理。

I think you would have this problem with a database too - except that I guess it would be handled by whatever method you use to migrate your existing databases to the latest version (ie adding a new column to a RDBMS table or attribute to ODBMS objects). 我想你也会遇到这个数据库的问题 - 除了我想它会用你用来将现有数据库迁移到最新版本的任何方法处理(即将新列添加到RDBMS表或属性到ODBMS对象) 。

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

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