3
votes

I have a very simple class that I'm trying to serialize:

    [ProtoContract]
public class SettingStore {
    public event EventHandler ContentsChanged;

    [ProtoMember(1)]
    private Dictionary<string, string> _StringVals = new Dictionary<string, string>(50);

    [ProtoMember(2)]
    private Dictionary<string, int> _IntVals = new Dictionary<string, int>(50);

    public SettingStore() {
    }

    //Bunch of accessors omited

    public static SettingStore DeSerialize(Stream data) {
        return Serializer.Deserialize<SettingStore>(data);
    }

    public void Serialize(Stream Target) {
        Serializer.Serialize<SettingStore>(Target, this);
    }
}

On serialize, I get a FieldAccessException with the following stack trace:

at System.Reflection.RuntimeFieldInfo.GetValue(Object obj) at ProtoBuf.Serializers.FieldDecorator.Write(Object value, ProtoWriter dest) at ProtoBuf.Serializers.TypeSerializer.Write(Object value, ProtoWriter dest) at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value) at ProtoBuf.Serializer.Serialize[T](Stream destination, SettingStore instance)

On deserialize I get the equivelant exception.

If I make the two dictionaries public, everything works fine, but totally breaks my object structure. I'm using the v2 r431 distribution. The same exact code works just fine in .NET 4.0.

Thanks!

1

1 Answers

3
votes

In WP7, accessing non-public members via reflection is limited to the assembly it's defined in. It's a security feature designed to prevent access to internal phone APIs.

Your _IntVals member is private, and thus can't be access by the proto-buffers assembly.