[帮助:序列化]带列表的自定义类 [英] [Help: Serialize] Custom Class With Lists

查看:88
本文介绍了[帮助:序列化]带列表的自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我创建了一个程序,旨在在更改日志时增加一些便利.由于我真的不想要任何本来可以付费购买的复杂应用程序,所以我决定自己制作一个.我唯一不知道要做的事情是序列化以保存自定义类的当前状态,该类具有针对每种更改类型(添加,删除等)的6个列表,以及一个用于跟踪该版本的字符串.

如果创建了新版本,则先前的(整个类)将添加到要在当前版本下添加的版本列表中.

我打算使用menuStrip控件来处理这些不同更改的打开/保存.
我只是不确定如何处理序列化以使所有信息都可用于存储.

我已经读过一些有关序列化的文章,但是我仍然感到困惑和沮丧.我所知道的是,您将"[Serializable]"添加到课程的开头,然后我迷路了,因为我不确定如何处理列表.

触发"save"和"load"方法的两个事件是从menuStrip发生的单击事件.


在主窗体上,它只是检查版本是否已更改,如果为true,则将旧版本添加到列表中,这是我不确定如何添加到保存"文件的另一部分.

Hello there, I created a program that is designed to add a bit of ease when it comes to change logs. Since I really didn''t want anything over complicated that could''ve been a paid for app I decided to make my own. The only thing I have no idea to do is Serializing to save the current state of my custom class that has 6 lists that are for each type of change(Added, Removed, etc.) and one string used for keeping track of that version.

If a new version is created then the previous one(the whole class) gets added to a list of versions to add under the current version.

I intend to use the menuStrip control for handling the Opening/Saving of these different changes.
I''m just unsure how to handle serializing to make all the information available for storage.

I''ve read a few articles already about Serialization but I still remain quite confused and frustrated. All I know is you add "[Serializable]" to the beginning of the class and after that I get lost as I''m not sure how to handle the lists.

The two events that would trigger the "save" and "load" methods are click events that happen from the menuStrip.


On the main Form it just checks if the version has changed and if true adds the old version to a list which is another part I''m unsure of how to add to the "save" file.

// pseudo-code-ish
private void btnChange_Click(object sender, EventArgs e)
{
    if(currentVersion is Old)
    {
        listOldVersion.Add(currentVersion);
    }
    currentVersion = new ChangeHandle(new version number);
}




ChangeHandle类




ChangeHandle Class

[Serializable]
public class ChangeHandle
{
    #region Fields

    private string sVersion;
    private List<string> listAdded;
    private List<string> listRemoved;
    private List<string> listBug;
    private List<string> listFixed;
    private List<string> listChanged;
    private List<string> listOther;

    #endregion

    #region Properties

    public string Version { get { return sVersion; } }

    #endregion

    #region Constructor

    public ChangeHandle(string Version)
    {
        sVersion = Version;
        listAdded = new List<string>();
        listRemoved = new List<string>();
        listBug = new List<string>();
        listFixed = new List<string>();
        listChanged = new List<string>();
        listOther = new List<string>();
    }

    #endregion

    #region Public Methods

    public void Added(string Change)
    {
        listAdded.Add(Change);
    }

    public void Removed(string Change)
    {
        listRemoved.Add(Change);
    }

    public void Bug(string Change)
    {
        listBug.Add(Change);
    }

    public void Fixed(string Change)
    {
        listFixed.Add(Change);
    }

    public void Changed(string Change)
    {
        listChanged.Add(Change);
    }

    public void Other(string Change)
    {
        listOther.Add(Change);
    }

    public StringBuilder Compile()
    {
        StringBuilder sbFinal = new StringBuilder();

        sbFinal.AppendLine();
        sbFinal.AppendLine(string.Format("Version: {0}",sVersion));

        foreach (string s in listAdded)
        {
            sbFinal.AppendLine(string.Format("  [Added]   - {0}", s));
        }
        foreach (string s in listRemoved)
        {
            sbFinal.AppendLine(string.Format("  [Removed] - {0}",s));
        }
        foreach (string s in listBug)
        {
            sbFinal.AppendLine(string.Format("  [Bug]     - {0}", s));
        }
        foreach (string s in listFixed)
        {
            sbFinal.AppendLine(string.Format("  [Fixed]   - {0}", s));
        }
        foreach (string s in listChanged)
        {
            sbFinal.AppendLine(string.Format("  [Changed] - {0}", s));
        }
        foreach (string s in listOther)
        {
            sbFinal.AppendLine(string.Format("  [Other]   - {0}", s));
        }

        return sbFinal;
    }

    #endregion
}




需要其他任何信息,请让我知道,我将尽快回发.

提前谢谢!





Any other information needed just let me know and I''ll post back asap.

Thank you in advance!


<ArrayOfChangeHandle xmlns="http://schemas.datacontract.org/2004/07/DevLog" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ChangeHandle/><ChangeHandle/></ArrayOfChangeHandle>

推荐答案

最好忘记基于序列化在Serializeable上并迁移到数据合同:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

这是一种真正一致的方法,最简单易用,但更加灵活.您不需要实现任何特殊的功能,只需注意接口,访问修饰符和继承.您可以用自己的方式实现数据类型,仅添加一些属性(除了序列化以外,其他所有属性都不会起作用).

请在我倡导这种方法的地方查看我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
It would be the best to forget serialization based on Serializeable and migrate to Data Contracts:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This is a really consistent approach, the easiest to use yet much more flexible. You don''t need to implement anything special, take care about interfaces, access modifiers, inheritance. You implement data types in your own way, only add some attributes (which cannot effect anything beyond serialization).

Please see my past answers 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


这篇关于[帮助:序列化]带列表的自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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