Beispiel #1
0
    public Test()
    {
        //如果使用了List<T>类型,T类型为自定义的Protobuf类则需全局仅注册一次Member中List<T>中T的类型,这个步骤必须在序列化之前注册,metaIndex与ProtoMember中的MemberTypeIndex对应
        ProtobufPropertyHelper.RegisterListMemberType(0, typeof(TestData));
        BaseProto proto = new BaseProto();

        proto.datas.Add(new TestData()
        {
            cid = 100
        });
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        ProtoBuf.Serializer.Serialize(stream, typeof(BaseProto), proto);
    }
        private IProtoSerializer BuildSerializer()
        {
            int opaqueToken = 0;

            try
            {
                model.TakeLock(ref opaqueToken);// check nobody is still adding this type
                WireType wireType;
                Type     finalType = itemType == null ? memberType : itemType;
                if (MemberTypeIndex != -1)
                {
                    finalType = ProtobufPropertyHelper.FindMemberType(MemberTypeIndex);
                }
                IProtoSerializer ser = TryGetCoreSerializer(model, dataFormat, finalType, out wireType, asReference, dynamicType, OverwriteList, true);
                if (ser == null)
                {
                    throw new InvalidOperationException("No serializer defined for type: " + finalType.FullName);
                }

                // apply tags
                if (itemType != null && SupportNull)
                {
                    if (IsPacked)
                    {
                        throw new NotSupportedException("Packed encodings cannot support null values");
                    }
                    ser = new TagDecorator(NullDecorator.Tag, wireType, IsStrict, ser);
                    ser = new NullDecorator(model, ser);
                    ser = new TagDecorator(fieldNumber, WireType.StartGroup, false, ser);
                }
                else
                {
                    ser = new TagDecorator(fieldNumber, wireType, IsStrict, ser);
                }
                bool flag = memberType.Name.Equals("String");
                if (flag)
                {
                }
                // apply lists if appropriate
                if (itemType != null && !flag)
                {
#if NO_GENERICS
                    Type underlyingItemType = itemType;
#else
                    Type underlyingItemType = SupportNull ? itemType : Helpers.GetUnderlyingType(itemType) ?? itemType;
#endif
                    Helpers.DebugAssert(underlyingItemType == ser.ExpectedType ||
                                        (ser.ExpectedType == model.MapType(typeof(object)) && !Helpers.IsValueType(underlyingItemType))
                                        , "Wrong type in the tail; expected {0}, received {1}", ser.ExpectedType, underlyingItemType);
                    if (memberType.IsArray)
                    {
                        ser = new ArrayDecorator(model, ser, fieldNumber, IsPacked, wireType, memberType, OverwriteList, SupportNull);
                    }
                    else
                    {
                        ser = ListDecorator.Create(model, memberType, defaultType, ser, fieldNumber, IsPacked, wireType, member != null && PropertyDecorator.CanWrite(model, member), OverwriteList, SupportNull);
                    }
                }
                else if (defaultValue != null && !IsRequired && getSpecified == null)
                {   // note: "ShouldSerialize*" / "*Specified" / etc ^^^^ take precedence over defaultValue,
                    // as does "IsRequired"
                    ser = new DefaultValueDecorator(model, defaultValue, ser);
                }
                if (memberType == model.MapType(typeof(Uri)))
                {
                    ser = new UriDecorator(model, ser);
                }
#if PORTABLE
                else if (memberType.FullName == typeof(Uri).FullName)
                {
                    // In PCLs, the Uri type may not match (WinRT uses Internal/Uri, .Net uses System/Uri)
                    ser = new ReflectedUriDecorator(memberType, model, ser);
                }
#endif
                if (member != null)
                {
                    PropertyInfo prop = member as PropertyInfo;
                    if (prop != null)
                    {
                        ser = new PropertyDecorator(model, parentType, (PropertyInfo)member, ser);
                    }
                    else
                    {
                        FieldInfo fld = member as FieldInfo;
                        if (fld != null)
                        {
                            ser = new FieldDecorator(parentType, (FieldInfo)member, ser);
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    if (getSpecified != null || setSpecified != null)
                    {
                        ser = new MemberSpecifiedDecorator(getSpecified, setSpecified, ser);
                    }
                }
                return(ser);
            }
            finally
            {
                model.ReleaseLock(opaqueToken);
            }
        }