2016-05-01 17 views
-1

BinaryFormatterを使用してDeSerialiseを実行するとき、私は正しいオブジェクト型を取得することがわかります。どのような種類のオブジェクトが保持されているDeSerialiseをBinaryFormatterが知っていますか?

オブジェクトが別のオブジェクトから継承されていても、BinaryFormatterは正しいオブジェクトをどのように保持しますか? DeSerialiseで正しいオブジェクトを作成するにはどうすればよいですか?

例:

[Serializable] 
public class Class1 
{ 
    public string Id { get; set; } 
    public int Age { get; set; } 
} 

[Serializable] 
public class Class2 : Class1 
{ 
    public string Id1 { get; set; } 
    public int Age1 { get; set; } 
} 

BinaryFormatter bf = new BinaryFormatter(); 

Class1 c1 = new Class1(); 
Class2 c2 = new Class2(); 

byte[] c1Bytes; 
byte[] c2Bytes; 

using (MemoryStream ms = new MemoryStream()) 
{ 
    bf.Serialize(ms, c2); 
    c2Bytes = ms.GetBuffer(); 
} 

using (MemoryStream ms = new MemoryStream(c2Bytes)) 
{ 
    var t = bf.Deserialize(ms); 

    string rr = t.ToString(); // rr contain "Class2" 

} 

答えて

2

BinaryFormatter格納種別情報であるため直列化されたデータ書式指定を確認できますhere

2

は(可変c2bytesにVisual Studioのメモリビューアを使用)c2bytesの内容を検査し、Google .net reflection

関連する問題