将类实例存储到文件/数据库的最佳方法 [英] The best way to store class instances to a file/database

查看:64
本文介绍了将类实例存储到文件/数据库的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将类的实例存储到文件/数据库的最佳方法是什么?

What is the best way to store instances of a class to file/database?

我们有一个称为Command的基类和许多派生类.用户通过向图形设计器中添加命令来创建这些类的实例他们可以在哪里配置它们.(设置属性).

We have a base class called Command and loads of derived classes. Users create instances of these classes by adding commands to a graphical designer where they can configure them. (Set the properties).

然后,我们需要一种将这些命令"存储到文件中而又不会丢失的方法任何信息.

We then need a way to store these "commands" to a file without losing any information.

一个想法是使用db4o,但是该项目不接受GPL许可证.

One idea was to use db4o, but the GPL license is not acceptable for this project.

有任何建议或代码示例吗?

Any suggestions or code samples?

更新:

(为了消除模糊"我的问题:p)

(In order to "de-blurryfie" my question :p)

生成的代码可能类似于:

The generated code might look something like:

    command[i++] = new DelayInSecondsCommand(2);
    command[i++] = new DaliRequestCommand(1, false, 254);
    command[i++] = new DaliRequestCommand(2, false, 254);
    command[i++] = new DaliRequestCommand(3, false, 254);
    command[i++] = new WaitInSecondsCommand(2);             
    command[i++] = new DaliRequestCommand(1, false, 0);
    command[i++] = new DaliRequestCommand(2, false, 0);
    command[i++] = new DaliRequestCommand(3, false, 0);
    command[i++] = new JumpCommand(0);

但随后会加载不同的命令.

But then with loads of different commands.

我知道.NET序列化是可能的,尽管我以前从未使用过它,但是我想知道是否还有更好的选择,比如我说db4o看起来不错,但是许可证不适合该项目.

I know it's possible with .NET serialization, altough I've never used it before, but I was wondering if there are better alternatives, like I said db4o seems nice but the license doesn't fit the project.

更新2:

感谢您的答复.我现在可能会使用序列化解决方案,但我也会研究其他选项.供参考.数据存储在SQL Compact数据库中.

Thank you for the replies. I'll probably go with the serialization solution now, but I'll look into the other options as well. F.Y.I. data is stored in a SQL Compact database.

推荐答案

序列化可以解决问题!序列化无非就是将一个对象或对象的连接图转换成字节流(以保持对象的当前状态).这可以是二进制流,XML或其他任何形式.由于.Net对序列化提供了强大的支持,因此您不必自己进行转换.序列化对象后,就可以将这些数据自由存储到文件或数据库中.同样,可以将代表序列化对象的字节流反序列化为一个对象,该对象将具有与原始对象相同的状态.

serialization does the trick! Serialization is nothing more than converting an object or a connected graph of objects into a stream of bytes (in order to persist the current state of the object). This can be a binary stream, XML or whatever. You don't have to do this conversion by your own since .Net has great support for serialization. Once you serialized an object, you are free to store this data to a file or database. Likewise, a stream of bytes representing a serialized object can be deserialized into an object which will have the same state as the original one.

Btw:有了序列化的字节流后,您可以在其上应用更多功能,例如压缩或加密.

Btw: Once you have a serialized stream of bytes, you can apply some more functions on it, e.g. compression or encryption.

这篇关于将类实例存储到文件/数据库的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆