示例#1
0
        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;
                IProtoSerializer ser       = TryGetCoreSerializer(model, dataFormat, finalType, out wireType, asReference, dynamicType, OverwriteList);
                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(ser);
                    ser = new TagDecorator(fieldNumber, WireType.StartGroup, false, ser);
                }
                else
                {
                    ser = new TagDecorator(fieldNumber, wireType, IsStrict, ser);
                }
                // apply lists if appropriate
                if (itemType != null)
                {
#if NO_GENERICS
                    Type underlyingItemType = itemType;
#else
                    Type underlyingItemType = SupportNull ? itemType : Nullable.GetUnderlyingType(itemType) ?? itemType;
#endif
                    Helpers.DebugAssert(underlyingItemType == ser.ExpectedType, "Wrong type in the tail; expected {0}, received {1}", ser.ExpectedType, underlyingItemType);
                    if (memberType.IsArray)
                    {
                        ser = new ArrayDecorator(ser, fieldNumber, IsPacked, wireType, memberType, OverwriteList, SupportNull);
                    }
                    else
                    {
                        ser = new ListDecorator(memberType, defaultType, ser, fieldNumber, IsPacked, wireType, member == null || PropertyDecorator.CanWrite(member), OverwriteList, SupportNull);
                    }
                }
                else if (defaultValue != null && !IsRequired)
                {
                    ser = new DefaultValueDecorator(defaultValue, ser);
                }
                if (memberType == typeof(Uri))
                {
                    ser = new UriDecorator(ser);
                }
                if (member != null)
                {
                    switch (member.MemberType)
                    {
                    case MemberTypes.Property:
                        ser = new PropertyDecorator(parentType, (PropertyInfo)member, ser); break;

                    case MemberTypes.Field:
                        ser = new FieldDecorator(parentType, (FieldInfo)member, ser); break;

                    default:
                        throw new InvalidOperationException();
                    }
                    if (getSpecified != null || setSpecified != null)
                    {
                        ser = new MemberSpecifiedDecorator(getSpecified, setSpecified, ser);
                    }
                }
                return(ser);
            }
            finally
            {
                model.ReleaseLock(opaqueToken);
            }
        }
示例#2
0
        private IProtoSerializer BuildSerializer()
        {
            bool lockTaken = false;

            try
            {
                model.TakeLock(ref lockTaken);// check nobody is still adding this type
                WireType         wireType;
                Type             finalType = itemType == null ? memberType : itemType;
                IProtoSerializer ser       = TryGetCoreSerializer(model, dataFormat, finalType, out wireType, asReference, dynamicType);
                if (ser == null)
                {
                    throw new InvalidOperationException("No serializer defined for type: " + finalType.FullName);
                }
                // apply tags
                ser = new TagDecorator(fieldNumber, wireType, IsStrict, ser);
                // apply lists if appropriate
                if (itemType != null)
                {
                    Helpers.DebugAssert(itemType == ser.ExpectedType, "Wrong type in the tail");
                    if (memberType.IsArray)
                    {
                        ser = new ArrayDecorator(ser, fieldNumber, IsPacked, wireType, memberType, OverwriteList);
                    }
                    else
                    {
                        ser = new ListDecorator(memberType, defaultType, ser, fieldNumber, IsPacked, wireType, member == null || PropertyDecorator.CanWrite(member), OverwriteList);
                    }
                }
                else if (defaultValue != null && !IsRequired)
                {
                    ser = new DefaultValueDecorator(defaultValue, ser);
                }
                if (memberType == typeof(Uri))
                {
                    ser = new UriDecorator(ser);
                }
                if (member != null)
                {
                    switch (member.MemberType)
                    {
                    case MemberTypes.Property:
                        ser = new PropertyDecorator(parentType, (PropertyInfo)member, ser); break;

                    case MemberTypes.Field:
                        ser = new FieldDecorator(parentType, (FieldInfo)member, ser); break;

                    default:
                        throw new InvalidOperationException();
                    }
                    if (getSpecified != null || setSpecified != null)
                    {
                        ser = new MemberSpecifiedDecorator(getSpecified, setSpecified, ser);
                    }
                }
                return(ser);
            }
            finally
            {
                model.ReleaseLock(lockTaken);
            }
        }
示例#3
0
        private IProtoSerializer BuildSerializer()
        {
            int opaqueToken = 0;

            try
            {
                model.TakeLock(ref opaqueToken);
                Type             type            = (itemType == null) ? memberType : itemType;
                IProtoSerializer protoSerializer = TryGetCoreSerializer(model, dataFormat, type, out WireType defaultWireType, asReference, dynamicType, OverwriteList, allowComplexTypes: true);
                if (protoSerializer == null)
                {
                    throw new InvalidOperationException("No serializer defined for type: " + type.FullName);
                }
                if (itemType != null && SupportNull)
                {
                    if (IsPacked)
                    {
                        throw new NotSupportedException("Packed encodings cannot support null values");
                    }
                    protoSerializer = new TagDecorator(1, defaultWireType, IsStrict, protoSerializer);
                    protoSerializer = new NullDecorator(model, protoSerializer);
                    protoSerializer = new TagDecorator(fieldNumber, WireType.StartGroup, strict: false, protoSerializer);
                }
                else
                {
                    protoSerializer = new TagDecorator(fieldNumber, defaultWireType, IsStrict, protoSerializer);
                }
                if (itemType != null)
                {
                    if (!SupportNull)
                    {
                        if (Helpers.GetUnderlyingType(itemType) == null)
                        {
                            _ = itemType;
                        }
                    }
                    else
                    {
                        _ = itemType;
                    }
                    protoSerializer = ((!memberType.IsArray) ? ((ProtoDecoratorBase)ListDecorator.Create(model, memberType, defaultType, protoSerializer, fieldNumber, IsPacked, defaultWireType, member != null && PropertyDecorator.CanWrite(model, member), OverwriteList, SupportNull)) : ((ProtoDecoratorBase) new ArrayDecorator(model, protoSerializer, fieldNumber, IsPacked, defaultWireType, memberType, OverwriteList, SupportNull)));
                }
                else if (defaultValue != null && !IsRequired && getSpecified == null)
                {
                    protoSerializer = new DefaultValueDecorator(model, defaultValue, protoSerializer);
                }
                if (memberType == model.MapType(typeof(Uri)))
                {
                    protoSerializer = new UriDecorator(model, protoSerializer);
                }
                if (member != null)
                {
                    if (member is PropertyInfo)
                    {
                        protoSerializer = new PropertyDecorator(model, parentType, (PropertyInfo)member, protoSerializer);
                    }
                    else
                    {
                        if (!(member is FieldInfo))
                        {
                            throw new InvalidOperationException();
                        }
                        protoSerializer = new FieldDecorator(parentType, (FieldInfo)member, protoSerializer);
                    }
                    if (getSpecified != null || setSpecified != null)
                    {
                        protoSerializer = new MemberSpecifiedDecorator(getSpecified, setSpecified, protoSerializer);
                    }
                }
                return(protoSerializer);
            }
            finally
            {
                model.ReleaseLock(opaqueToken);
            }
        }