2013-08-19 80 views
0

親愛的堆棧溢出社區, 我想我永遠不會問這個問題,但顯然是序列化或我的用法出了問題。對象序列化問題(c#)

我有一個類,我需要序列:

[Serializable] 
public class Device : ISerializable, IDisposable 
{ 
    public delegate void ListChangedEventHandler(object sender, ListChangedEventArgs e); 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string IPAddress { get; set; } 

    [Browsable(false)] 
    public ThreadSafeBindingList<Item> Items { get; set; } 

    [Browsable(false)] 
    public MibEntity AssociatedMibEntity { get; set; } 

    //methods etc. 
} 

一點解釋:

ThreadSafeBindingList從System.ComponentModel.BindingList MibEntity繼承是一類SharpSNMP庫(http://sharpsnmplib.codeplex.com/

問題: 當我嘗試t反序列化對象,MibEntity始終爲空。其他屬性都很好。 MibEntity類在外部的dll中,我在Device類所在的項目中引用它。

這是它的內容:

[Serializable] 
public class MibEntity 
{ 
    public MibEntity() 
    { 
     Children = new List<MibEntity>(); 
    } 

    [Browsable(false)] 
    public List<MibEntity> Children { get; set; } 

    [Browsable(true)] 
    [Category("General")] 
    public string OID { get; set; } 

    private string _name; 
    [Browsable(true)] 
    [Category("General")] 
    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 

    [Browsable(true)] 
    public string Description { get; set; } 

    [Browsable(false)] 
    public int Value { get; set; } 


    [Browsable(true)] 
    public AccessType AccessType { get; set; } //this is enum from SharpSNMP library 

    [Browsable(true)] 
    public Status Status { get; set; } //this is enum from same assembly as this class 

    [Browsable(true)] 
    public string Syntax { get; set; } 

    [Browsable(true)] 
    public bool IsTableEntry { get; set; } 

    [Browsable(true)] 
    public IDefinition IDefinition { get; set; } //this is interface from SharpSNMP library 
} 

我使用的BinaryFormatter序列化和反序列化。 謝謝你的幫助!

+5

由於您通過'ISerializable'自定義序列化,我們需要同時看到自定義的'GetObjectData'方法和自定義的反序列化構造函數。 –

回答

3

這總是意味着您在自定義ISerializable實施中發生了錯誤;以下工作正常:

protected Device(SerializationInfo info, StreamingContext context) 
{ 
    Name = info.GetString("Name"); 
    //... 
    AssociatedMibEntity = (MibEntity)info.GetValue(
     "AssociatedMibEntity", typeof(MibEntity)); 
} 
void ISerializable.GetObjectData(
    SerializationInfo info, StreamingContext context) 
{ 
    info.AddValue("Name", Name); 
    //... 
    info.AddValue("AssociatedMibEntity", AssociatedMibEntity); 
} 

與驗證:

using (var ms = new MemoryStream()) 
{ 
    var bf = new BinaryFormatter(); 
    bf.Serialize(ms, new Device { 
     AssociatedMibEntity = new MibEntity { Name = "Foo"}}); 
    ms.Position = 0; 
    var clone = (Device)bf.Deserialize(ms); 
    Console.WriteLine(clone.AssociatedMibEntity.Name); // Foo 
} 

不過,除非是實現ISerializable一個很好的理由,你可能會得到更好的通過簡單地完全去除ISerializable實施服務 - 提這將是一個重大改變(如果你這樣做,任何現有的數據都不會正確地反序列化)。

[Serializable] 
public class Device : IDisposable // <=== no ISerializable; also removed 
            // GetObjectData and custom .ctor 

另外:我一貫的咆哮:BinaryFormatter可能不是你最好的選擇,如果你在任何地方存儲這些數據。它是... 。有一系列更可靠的序列化器。