一个类必须在多高的层次上标记为[可序列化]? [英] How far up the hierarchy does a class have to be marked [Serializable]?

查看:89
本文介绍了一个类必须在多高的层次上标记为[可序列化]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简短的问题:

为什么必须将此类(TCtsPageAdmin)标记为[可序列化]?
我只是想序列化

Very short question:

Why does this class(TCtsPageAdmin) have to be marked [Serializable]?
I''m just trying to serialize a

List<TCtsPage>

TCtsPage类已正确标记.

如果我使用[Serializable]属性正确标记了TCtsPageAdmin,则该错误将层次结构上移->.使用TCtsPageadmin对象的对象将成为错误的目标.

错误:
SaveToFile失败:
在程序集``LrjCtsAdmin,Version = 1.0.1.2,
中键入''Lrj.Cts.Admin.TCtsPageAdmin'' 文化=中性,PublicKeyToken = null''未标记为可序列化.


TCtsPage class is marked properly.

If I mark TCtsPageAdmin properly with the [Serializable] attribute, then the error moves up the hierarchy -> the object that consumes a TCtsPageadmin object will be the target of the error.

Error:
SaveToFile failed:
Type ''Lrj.Cts.Admin.TCtsPageAdmin'' in Assembly ''LrjCtsAdmin, Version=1.0.1.2,
Culture=neutral, PublicKeyToken=null'' is not marked as serializable.


[CLSCompliant(true)]
public sealed class TCtsPageAdmin : ILrjObj
{
 ...
    public void SaveToFile(bool aUsePagePath = true)
    {
        String aFileName;
        List<TCtsPage> lst = GetPages("*");

        aFileName = aUsePagePath ? Path.Combine(PagePath, "pages.dat") : 
                                                                    "pages.dat";

        BinaryFormatter binForm = new BinaryFormatter();
        try
        {
            using (Stream fStream = new FileStream(aFileName, FileMode.Create, 
                                              FileAccess.Write, FileShare.None))
            {
                binForm.Serialize(fStream, lst);
                OnSavedToFile(
                  new InfoEventArgs(
                      String.Format("SaveToFile succeeded.\n{0}", aFileName)));
            }
        }
        catch (Exception ex)
        {
            OnSaveToFileFailed(
              new InfoEventArgs(
                    String.Format("SaveToFile failed: \n{0}", ex.Message)));
        }
    }
 ...
}

推荐答案

TCtsPage中使用的所有对象都必须具有[Serializable]属性.

另外,如果您可以使用不需要任何东西并且比BinaryFormatter更快的序列化器,则可以:
fastJSON [ ^ ]

fastBinaryJSON [ ^ ]
All object used in TCtsPage must have a [Serializable] attribute.

Alternatively if you can use my serializers which don''t require anything and are faster than the BinaryFormatter:
fastJSON[^]

fastBinaryJSON[^]


这篇关于一个类必须在多高的层次上标记为[可序列化]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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