如何保存/加载对象 [英] How to save/load an object

查看:73
本文介绍了如何保存/加载对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个此类(树的递归定义),我想为它提供一个保存/加载机制(在xml文件或sql server数据库上).最好的方法是什么?

Hi, I have this class (a recursive definition of a tree) which I wana provide a save/load mechanism for it(on an xml file or sql server database). What is the best way to do this?

public class TaskViewModel : INotifyPropertyChanged
{
    private Task vmTask;    
    private ObservableCollection<TaskViewModel> vmSubTasks;
    TaskViewModel vmParent;
    bool vmIsExpanded;
    bool vmIsSelected;
    public bool VmIsVisited { get; set; }

    //ctors
    public TaskViewModel(Task task, TaskViewModel prnt)
    {
        VmTask = task;  //:Me vmTask = task;
        Parent = prnt; //:Me vmParent = parent; 
        ///<summary>
        ///recursively walks down the Task tree, wrapping each Task object in a TaskViewModel
        ///</summary>
        SubTasks = new ObservableCollection<TaskViewModel>( //:Me vmSubTasks = new ObservableCollection<TaskViewModel>(
                (from t in vmTask.SubTasks
                 select new TaskViewModel(t, this)).ToList<TaskViewModel>());          
    }
    public TaskViewModel(Task task)
        : this(task, null)
    { }
    public bool IsExpanded
    {
        get { return vmIsExpanded; }
        set
        {
            if (value != vmIsExpanded)
            {
                vmIsExpanded = value;
                this.OnPropertyChanged("IsExpanded");
            }
            // Expand all the way up to the root.
            if (vmIsExpanded && vmParent != null)
                Parent.IsExpanded = true;   //fantastic! (executes the setter again)
        }
    }
    public bool IsSelected
    {
        get { return vmIsSelected; }
        set
        {
            if (value != vmIsSelected)
            {
                vmIsSelected = value;
                this.OnPropertyChanged("IsSelected");
                OnPropertyChanged("Name");  //required: reflects the change of txtName to the corresponding TreeViewItem name  
                //OnPropertyChanged is only required for the Name property
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    } 
}

推荐答案

最好的装载和存储对象的方法是使用 Data Contract :
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

请在我倡导这种方法的情况下查看我过去对类似问题的回答:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
The best way of loading and storing object is using Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see my past answers to similar questions where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


以下是我发现的一个快速而肮脏的示例: ^ ]
Here is as quick and dirty an example as I could find: http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file[^]


这篇关于如何保存/加载对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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