2009-07-31 74 views
2

下面的簡單類繼承自HashSet,因此必須實現ISerialization成員(以非標準方式)。我得到下面的異常,當我試圖序列化然後反序列化集團的一個實例:請向我解釋此SerializationException

試驗方法 UtilitiesTests.GroupTest.SerializeTest 拋出異常: System.Reflection.TargetInvocationException: 的Het doel麪包車EEN aanroep heeft EEN uitzondering veroorzaakt。 ---> System.Runtime.Serialization.SerializationException: 蓋nameprop是Niet的已找到..

不幸的是,這是在荷蘭。這意味着無法找到成員「nameprop」! 什麼是錯?

using System; 
using System.Collections.Generic; 
using System.Runtime.Serialization; 

namespace Grouping 
{ 
    [Serializable] 
    public class Group<T> : HashSet<T> 
    { 
     public Group(string name) 
     { 
      Name = name; 
     } 

     protected Group(){} 

     protected Group(SerializationInfo info, StreamingContext context):base(info,context) 
     { 
      Name = info.GetString("nameprop"); 
     } 

     protected new void GetObjectData(SerializationInfo info,StreamingContext context) 
     { 
      base.GetObjectData(info,context); 
      info.AddValue("nameprop", Name); 
     } 

     public string Name { get; private set; } 
    } 
} 
+0

如果您需要抑制錯誤,請從http://lifeelement.com結帳。 SerializationException未找到成員
http://lifeelement.com/news/serializationexception-member-was-not-found/。 – 2012-05-05 18:49:02

回答

6

GetObjectData方法永遠不會序列化過程中調用,因爲你沒有重載父類的方法 - 你的影子吧。那裏應該使用override而不是new

+0

謝謝!就是這樣... 你能指點我的英文錯誤信息,以便讓這個問題更容易找到像我這樣的其他傻瓜嗎? – Dabblernl 2009-07-31 21:32:11