请向我解释一下这个SerializationException

Dab*_*rnl 2 .net c# serialization

下面的简单类继承自HashSet,因此必须实现ISerialization成员(以非标准方式).当我尝试序列化然后反序列化Group的实例时,我得到以下异常:

测试方法UtilitiesTests.GroupTest.SerializeTest抛出异常:System.Reflection.TargetInvocationException:Het doel van een aanroep heeft een uitzondering veroorzaakt.---> System.Runtime.Serialization.SerializationException:Lid nameprop是niet gevonden ..

不幸的是,这是荷兰语.这意味着无法找到成员"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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*aev 6

GetObjectData在序列化期间永远不会调用您的方法,因为您不会覆盖父方法 - 您将其隐藏.你应该使用override而不是new那里.