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

时间:2014-03-29 14:34:32

标签: c# serialization

说我有以下课程:

[Serializable]
public class foo
{
    public bar randomBar;

    // required serialization methods would go here
}

并且

public class bar
{
    int barInt;

    // constructors and such
}

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

1 个答案:

答案 0 :(得分:0)

序列化程序抛出错误。在LinqPad中尝试以下内容:

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; }

错误是:

Type 'UserQuery+Bar' in Assembly 'query_nsalab, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
相关问题