首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可序列化对象的不可序列化成员是否序列化?

可序列化对象的不可序列化成员是否序列化?
EN

Stack Overflow用户
提问于 2014-03-29 14:34:31
回答 1查看 48关注 0票数 0

假设我有以下课程:

代码语言:javascript
复制
[Serializable]
public class foo
{
    public bar randomBar;

    // required serialization methods would go here
}

代码语言:javascript
复制
public class bar
{
    int barInt;

    // constructors and such
}

bar是不可序列化的,但foo是可序列化的。当我序列化一个foo对象时,它的randomBar会被序列化,还是会导致错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-29 15:16:40

序列化程序抛出一个错误。在LinqPad中尝试以下操作:

代码语言:javascript
复制
void Main()
{
    var stream = new MemoryStream();
    var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    formatter.Serialize(stream, new Foo { bar = new Bar { barInt = 2 } });

}

// Define other methods and classes here
[Serializable]
public class Foo { public Bar bar; }

public class Bar { public int barInt; }

错误是:

代码语言:javascript
复制
Type 'UserQuery+Bar' in Assembly 'query_nsalab, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22732411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档