/// <summary>

        /// Creates a new ValueMember instance

        /// </summary>

        public ValueMember(RuntimeTypeModel model, Type parentType, int fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaultType, DataFormat dataFormat, object defaultValue)

            : this(model, fieldNumber, memberType, itemType, defaultType, dataFormat)

        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            if (parentType == null)
            {
                throw new ArgumentNullException("parentType");
            }

            if (fieldNumber < 1 && !Helpers.IsEnum(parentType))
            {
                throw new ArgumentOutOfRangeException("fieldNumber");
            }



            this.member = member;

            this.parentType = parentType;

            if (fieldNumber < 1 && !Helpers.IsEnum(parentType))
            {
                throw new ArgumentOutOfRangeException("fieldNumber");
            }

//#if WINRT

            if (defaultValue != null && model.MapType(defaultValue.GetType()) != memberType)

//#else

//            if (defaultValue != null && !memberType.IsInstanceOfType(defaultValue))

//#endif

            {
                defaultValue = ParseDefaultValue(memberType, defaultValue);
            }

            this.defaultValue = defaultValue;



            MetaType type = model.FindWithoutAdd(memberType);

            if (type != null)

            {
                this.asReference = type.AsReferenceDefault;
            }

            else

            { // we need to scan the hard way; can't risk recursion by fully walking it
                this.asReference = MetaType.GetAsReferenceDefault(model, memberType);
            }
        }
Beispiel #2
0
        internal static IProtoSerializer TryGetCoreSerializer(RuntimeTypeModel model, DataFormat dataFormat, Type type, out WireType defaultWireType,
                                                              bool asReference, bool dynamicType, bool overwriteList, bool allowComplexTypes)
        {
#if !NO_GENERICS
            {
                Type tmp = Helpers.GetUnderlyingType(type);
                if (tmp != null)
                {
                    type = tmp;
                }
            }
#endif
            if (Helpers.IsEnum(type))
            {
                if (allowComplexTypes && model != null)
                {
                    // need to do this before checking the typecode; an int enum will report Int32 etc
                    defaultWireType = WireType.Variant;
                    return(new EnumSerializer(type, model.GetEnumMap(type)));
                }
                else
                { // enum is fine for adding as a meta-type
                    defaultWireType = WireType.None;
                    return(null);
                }
            }
            ProtoTypeCode code = Helpers.GetTypeCode(type);
            switch (code)
            {
            case ProtoTypeCode.Int32:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new Int32Serializer(model));

            case ProtoTypeCode.UInt32:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new UInt32Serializer(model));

            case ProtoTypeCode.Int64:
                defaultWireType = GetIntWireType(dataFormat, 64);
                return(new Int64Serializer(model));

            case ProtoTypeCode.UInt64:
                defaultWireType = GetIntWireType(dataFormat, 64);
                return(new UInt64Serializer(model));

            case ProtoTypeCode.String:
                defaultWireType = WireType.String;
                if (asReference)
                {
                    return(new NetObjectSerializer(model, model.MapType(typeof(string)), 0, BclHelpers.NetObjectOptions.AsReference));
                }
                return(new StringSerializer(model));

            case ProtoTypeCode.Single:
                defaultWireType = WireType.Fixed32;
                return(new SingleSerializer(model));

            case ProtoTypeCode.Double:
                defaultWireType = WireType.Fixed64;
                return(new DoubleSerializer(model));

            case ProtoTypeCode.Boolean:
                defaultWireType = WireType.Variant;
                return(new BooleanSerializer(model));

            case ProtoTypeCode.DateTime:
                defaultWireType = GetDateTimeWireType(dataFormat);
                return(new DateTimeSerializer(dataFormat, model));

            case ProtoTypeCode.Decimal:
                defaultWireType = WireType.String;
                return(new DecimalSerializer(model));

            case ProtoTypeCode.Byte:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new ByteSerializer(model));

            case ProtoTypeCode.SByte:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new SByteSerializer(model));

            case ProtoTypeCode.Char:
                defaultWireType = WireType.Variant;
                return(new CharSerializer(model));

            case ProtoTypeCode.Int16:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new Int16Serializer(model));

            case ProtoTypeCode.UInt16:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new UInt16Serializer(model));

            case ProtoTypeCode.TimeSpan:
                defaultWireType = GetDateTimeWireType(dataFormat);
                return(new TimeSpanSerializer(dataFormat, model));

            case ProtoTypeCode.Guid:
                defaultWireType = dataFormat == DataFormat.Group ? WireType.StartGroup : WireType.String;
                return(new GuidSerializer(model));

            case ProtoTypeCode.Uri:
                defaultWireType = WireType.String;
                return(new StringSerializer(model));

            case ProtoTypeCode.ByteArray:
                defaultWireType = WireType.String;
                return(new BlobSerializer(model, overwriteList));

            case ProtoTypeCode.Type:
                defaultWireType = WireType.String;
                return(new SystemTypeSerializer(model));
            }
            IProtoSerializer parseable = model.AllowParseableTypes ? ParseableSerializer.TryCreate(type, model) : null;
            if (parseable != null)
            {
                defaultWireType = WireType.String;
                return(parseable);
            }
            if (allowComplexTypes && model != null)
            {
                int      key  = model.GetKey(type, false, true);
                MetaType meta = null;
                if (key >= 0)
                {
                    meta = model[type];
                    if (dataFormat == DataFormat.Default && meta.IsGroup)
                    {
                        dataFormat = DataFormat.Group;
                    }
                }

                if (asReference || dynamicType)
                {
                    BclHelpers.NetObjectOptions options = BclHelpers.NetObjectOptions.None;
                    if (asReference)
                    {
                        options |= BclHelpers.NetObjectOptions.AsReference;
                    }
                    if (dynamicType)
                    {
                        options |= BclHelpers.NetObjectOptions.DynamicType;
                    }
                    if (meta != null)
                    { // exists
                        if (asReference && Helpers.IsValueType(type))
                        {
                            string message = "AsReference cannot be used with value-types";

                            if (type.Name == "KeyValuePair`2")
                            {
                                message += "; please see http://stackoverflow.com/q/14436606/";
                            }
                            else
                            {
                                message += ": " + type.FullName;
                            }
                            throw new InvalidOperationException(message);
                        }

                        if (asReference && meta.IsAutoTuple)
                        {
                            options |= BclHelpers.NetObjectOptions.LateSet;
                        }
                        if (meta.UseConstructor)
                        {
                            options |= BclHelpers.NetObjectOptions.UseConstructor;
                        }
                    }
                    defaultWireType = dataFormat == DataFormat.Group ? WireType.StartGroup : WireType.String;
                    return(new NetObjectSerializer(model, type, key, options));
                }
                if (key >= 0)
                {
                    defaultWireType = dataFormat == DataFormat.Group ? WireType.StartGroup : WireType.String;
                    return(new SubItemSerializer(type, key, meta, true));
                }
            }
            defaultWireType = WireType.None;
            return(null);
        }
Beispiel #3
0
        internal static IProtoSerializer TryGetCoreSerializer(RuntimeTypeModel model, DataFormat dataFormat, Type type, out WireType defaultWireType,
                                                              bool asReference, bool dynamicType, bool overwriteList, bool allowComplexTypes)
        {
#if !NO_GENERICS
            type = Helpers.GetUnderlyingType(type) ?? type;
#endif
            if (Helpers.IsEnum(type))
            {
                if (allowComplexTypes && model != null)
                {
                    // need to do this before checking the typecode; an int enum will report Int32 etc
                    defaultWireType = WireType.Variant;
                    return(new EnumSerializer(type, model.GetEnumMap(type)));
                }
                else
                { // enum is fine for adding as a meta-type
                    defaultWireType = WireType.None;
                    return(null);
                }
            }
            ProtoTypeCode code = Helpers.GetTypeCode(type);
            switch (code)
            {
            case ProtoTypeCode.Int32:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new Int32Serializer(model));

            case ProtoTypeCode.UInt32:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new UInt32Serializer(model));

            case ProtoTypeCode.Int64:
                defaultWireType = GetIntWireType(dataFormat, 64);
                return(new Int64Serializer(model));

            case ProtoTypeCode.UInt64:
                defaultWireType = GetIntWireType(dataFormat, 64);
                return(new UInt64Serializer(model));

            case ProtoTypeCode.String:
                defaultWireType = WireType.String;
                if (asReference)
                {
                    return(new NetObjectSerializer(model, model.MapType(typeof(string)), 0, BclHelpers.NetObjectOptions.AsReference));
                }
                return(new StringSerializer(model));

            case ProtoTypeCode.Single:
                defaultWireType = WireType.Fixed32;
                return(new SingleSerializer(model));

            case ProtoTypeCode.Double:
                defaultWireType = WireType.Fixed64;
                return(new DoubleSerializer(model));

            case ProtoTypeCode.Boolean:
                defaultWireType = WireType.Variant;
                return(new BooleanSerializer(model));

            case ProtoTypeCode.DateTime:
                defaultWireType = GetDateTimeWireType(dataFormat);
                return(new DateTimeSerializer(model));

            case ProtoTypeCode.Decimal:
                defaultWireType = WireType.String;
                return(new DecimalSerializer(model));

            case ProtoTypeCode.Byte:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new ByteSerializer(model));

            case ProtoTypeCode.SByte:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new SByteSerializer(model));

            case ProtoTypeCode.Char:
                defaultWireType = WireType.Variant;
                return(new CharSerializer(model));

            case ProtoTypeCode.Int16:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new Int16Serializer(model));

            case ProtoTypeCode.UInt16:
                defaultWireType = GetIntWireType(dataFormat, 32);
                return(new UInt16Serializer(model));

            case ProtoTypeCode.TimeSpan:
                defaultWireType = GetDateTimeWireType(dataFormat);
                return(new TimeSpanSerializer(model));

            case ProtoTypeCode.Guid:
                defaultWireType = WireType.String;
                return(new GuidSerializer(model));

            case ProtoTypeCode.Uri:
                defaultWireType = WireType.String;
                return(new StringSerializer(model));    // treat as string; wrapped in decorator later

            case ProtoTypeCode.ByteArray:
                defaultWireType = WireType.String;
                return(new BlobSerializer(model, overwriteList));

            case ProtoTypeCode.Type:
                defaultWireType = WireType.String;
                return(new SystemTypeSerializer(model));
            }
            IProtoSerializer parseable = model.AllowParseableTypes ? ParseableSerializer.TryCreate(type, model) : null;
            if (parseable != null)
            {
                defaultWireType = WireType.String;
                return(parseable);
            }
            if (allowComplexTypes && model != null)
            {
                int key = model.GetKey(type, false, true);
                if (asReference || dynamicType)
                {
                    defaultWireType = WireType.String;
                    BclHelpers.NetObjectOptions options = BclHelpers.NetObjectOptions.None;
                    if (asReference)
                    {
                        options |= BclHelpers.NetObjectOptions.AsReference;
                    }
                    if (dynamicType)
                    {
                        options |= BclHelpers.NetObjectOptions.DynamicType;
                    }
                    if (key >= 0)
                    { // exists
                        if (model[type].UseConstructor)
                        {
                            options |= BclHelpers.NetObjectOptions.UseConstructor;
                        }
                    }
                    return(new NetObjectSerializer(model, type, key, options));
                }
                if (key >= 0)
                {
                    defaultWireType = dataFormat == DataFormat.Group ? WireType.StartGroup : WireType.String;
                    return(new SubItemSerializer(type, key, model[type], true));
                }
            }
            defaultWireType = WireType.None;
            return(null);
        }