views:

120

answers:

1

The simple class below inherits from HashSet and thus has to implement the ISerialization members (in a non standard way). I get the following Exception when I try to serialize then deserialize an instance of Group:

Test method UtilitiesTests.GroupTest.SerializeTest threw exception: System.Reflection.TargetInvocationException: Het doel van een aanroep heeft een uitzondering veroorzaakt. ---> System.Runtime.Serialization.SerializationException: Lid nameprop is niet gevonden..

Unfortunately this is in dutch. It means that the member "nameprop" could not be found! What is wrong??

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; }
    }
}
+4  A: 

Your GetObjectData method is never called during serialization, because you don't override the parent method - you shadow it. You should use override rather than new there.

Pavel Minaev
Thanks! That was it...Could you point me to the the english error message so as to make this question easier to find by other simpletons like me?
Dabblernl
I don't know as I didn't actually run the code, only looked at it :)
Pavel Minaev