private static bool TrySerializeAsPrimitiveValue(Type propertyType, ProtobufFieldAttribute attribute, out BuildingSerializer buildingSerializer) { buildingSerializer = null; SerializerWithWireType serializer; if ((attribute.DataType != DataType.Default && DataTypeToSerializerCache.TryGetSerializer(attribute.DataType, out serializer)) || DefaultSerializersCache.TryGetSerializer(propertyType, out serializer)) { { buildingSerializer = new BuildingSerializer(serializer, new SingleValueBuilder(propertyType)); return true; } } return false; }
private static bool TrySerializeAsCollection(Type propertyType, ProtobufFieldAttribute attribute, bool examineForCollection, out BuildingSerializer buildingSerializer) { buildingSerializer = null; if (examineForCollection && propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(ICollection<>)) { Type underlyingType = propertyType.GetGenericArguments()[0]; SerializerWithWireType underlyingSerializer = CreateBuildingSerializer( underlyingType, attribute, false).Serializer; { buildingSerializer = new BuildingSerializer(attribute.Packed ? (SerializerWithWireType)new PackedRepeatedSerializer(underlyingSerializer) : new RepeatedSerializer(attribute.FieldNumber, underlyingSerializer), new RepeatedValueBuilder(underlyingType, !attribute.Packed)); return true; } } return false; }
private static bool TrySerializeAsNullable(Type propertyType, ProtobufFieldAttribute attribute, out BuildingSerializer buildingSerializer) { buildingSerializer = null; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { Type underlyingType = propertyType.GetGenericArguments()[0]; SerializerWithWireType underlyingSerializer = CreateBuildingSerializer( underlyingType, attribute, false).Serializer; buildingSerializer = new BuildingSerializer( underlyingSerializer, new NullableValueBuilder(underlyingType)); return true; } return false; }
private static BuildingSerializer CreateBuildingSerializer(Type propertyType, ProtobufFieldAttribute attribute, bool examineForCollection) { BuildingSerializer buildingSerializer; if (TrySerializeAsPrimitiveValue(propertyType, attribute, out buildingSerializer)) { return buildingSerializer; } if (TrySerializeAsEnum(propertyType, out buildingSerializer)) { return buildingSerializer; } if (TrySerializeAsNullable(propertyType, attribute, out buildingSerializer)) { return buildingSerializer; } if (TrySerializeAsCollection(propertyType, attribute, examineForCollection, out buildingSerializer)) { return buildingSerializer; } return new BuildingSerializer(new EmbeddedMessageSerializer( CreateSerializer(propertyType)), new SingleValueBuilder(propertyType)); }