Beispiel #1
0
        internal MetaType(RuntimeTypeModel model, Type type)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            WireType         defaultWireType;
            IProtoSerializer coreSerializer = ValueMember.TryGetCoreSerializer(null, DataFormat.Default, type, out defaultWireType, false, false);

            if (coreSerializer != null)
            {
                throw new ArgumentException("Data of this type has inbuilt behaviour, and cannot be added to a model in this way: " + type.FullName);
            }

            this.type  = type;
            this.model = model;

            if (type.IsEnum)
            {
                EnumPassthru = type.IsDefined(typeof(FlagsAttribute), false);
            }
        }
        private void CascadeDependents(BasicList list, MetaType metaType)
        {
            MetaType surrogateOrBaseOrSelf;

            if (metaType.IsList)
            {
                Type listItemType = TypeModel.GetListItemType(this, metaType.Type);
                if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, listItemType, out WireType _, asReference: false, dynamicType: false, overwriteList: false, allowComplexTypes: false) != null)
                {
                    return;
                }
                int num = FindOrAddAuto(listItemType, demand: false, addWithContractOnly: false, addEvenIfAutoDisabled: false);
                if (num >= 0)
                {
                    surrogateOrBaseOrSelf = ((MetaType)types[num]).GetSurrogateOrBaseOrSelf(deep: false);
                    if (!list.Contains(surrogateOrBaseOrSelf))
                    {
                        list.Add(surrogateOrBaseOrSelf);
                        CascadeDependents(list, surrogateOrBaseOrSelf);
                    }
                }
                return;
            }
            if (metaType.IsAutoTuple)
            {
                if (MetaType.ResolveTupleConstructor(metaType.Type, out MemberInfo[] mappedMembers) != null)
Beispiel #3
0
        // Token: 0x060003F5 RID: 1013 RVA: 0x00014D68 File Offset: 0x00012F68
        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 (defaultValue != null && model.MapType(defaultValue.GetType()) != memberType)
            {
                defaultValue = ValueMember.ParseDefaultValue(memberType, defaultValue);
            }
            this.defaultValue = defaultValue;
            MetaType metaType = model.FindWithoutAdd(memberType);

            if (metaType != null)
            {
                this.asReference = metaType.AsReferenceDefault;
                return;
            }
            this.asReference = MetaType.GetAsReferenceDefault(model, memberType);
        }
Beispiel #4
0
 private void CascadeDependents(BasicList list, MetaType metaType)
 {
     if (metaType.IsList)
     {
         Type listItemType = TypeModel.GetListItemType(this, metaType.Type);
         if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, listItemType, out WireType _, false, false, false, false) == null)
         {
             int num = FindOrAddAuto(listItemType, false, false, false);
             if (num >= 0)
             {
                 MetaType surrogateOrBaseOrSelf = ((MetaType)types[num]).GetSurrogateOrBaseOrSelf(false);
                 if (!list.Contains(surrogateOrBaseOrSelf))
                 {
                     list.Add(surrogateOrBaseOrSelf);
                     CascadeDependents(list, surrogateOrBaseOrSelf);
                 }
             }
         }
     }
     else
     {
         MetaType surrogateOrBaseOrSelf;
         if (metaType.IsAutoTuple)
         {
             if (MetaType.ResolveTupleConstructor(metaType.Type, out MemberInfo[] array) != (ConstructorInfo)null)
Beispiel #5
0
 private void Add(ValueMember member)
 {
     lock (fields)
     {
         ThrowIfFrozen();
         fields.Add(member);
     }
 }
 private void SetFlag(byte flag, bool value, bool throwIfFrozen)
 {
     if (throwIfFrozen && this.HasFlag(flag) != value)
     {
         this.ThrowIfFrozen();
     }
     if (value)
     {
         ValueMember valueMember = this;
         valueMember.flags = (byte)(valueMember.flags | flag);
         return;
     }
     this.flags = (byte)(this.flags & ~flag);
 }
Beispiel #7
0
 internal EnumSerializer.EnumPair[] GetEnumMap()
 {
     if (enumPassthru)
     {
         return(null);
     }
     EnumSerializer.EnumPair[] result = new EnumSerializer.EnumPair[fields.Count];
     for (int i = 0; i < result.Length; i++)
     {
         ValueMember member    = (ValueMember)fields[i];
         int         wireValue = member.FieldNumber;
         Enum        value     = member.GetEnumValue();
         result[i] = new EnumSerializer.EnumPair(wireValue, value);
     }
     return(result);
 }
Beispiel #8
0
        internal IProtoSerializer TryGetBasicTypeSerializer(Type type)
        {
            int num = this.basicTypes.IndexOf(RuntimeTypeModel.BasicTypeFinder, type);

            if (num >= 0)
            {
                return(((RuntimeTypeModel.BasicType) this.basicTypes[num]).Serializer);
            }
            BasicList        basicList = this.basicTypes;
            IProtoSerializer result;

            lock (basicList)
            {
                num = this.basicTypes.IndexOf(RuntimeTypeModel.BasicTypeFinder, type);
                if (num >= 0)
                {
                    result = ((RuntimeTypeModel.BasicType) this.basicTypes[num]).Serializer;
                }
                else
                {
                    IProtoSerializer arg_9A_0;
                    if (MetaType.GetContractFamily(this, type, null) == MetaType.AttributeFamily.None)
                    {
                        WireType         wireType;
                        IProtoSerializer protoSerializer = ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out wireType, false, false, false, false);
                        arg_9A_0 = protoSerializer;
                    }
                    else
                    {
                        arg_9A_0 = null;
                    }
                    IProtoSerializer protoSerializer2 = arg_9A_0;
                    if (protoSerializer2 != null)
                    {
                        this.basicTypes.Add(new RuntimeTypeModel.BasicType(type, protoSerializer2));
                    }
                    result = protoSerializer2;
                }
            }
            return(result);
        }
Beispiel #9
0
        private ValueMember ApplyDefaultBehaviour(bool isEnum, AttributeFamily family, MemberInfo member, BasicList partialMembers)
        {
            if (member == null || (family == AttributeFamily.None && !isEnum))
            {
                return(null);                                                               // nix
            }
            Type effectiveType;

            switch (member.MemberType)
            {
            case MemberTypes.Field:
                effectiveType = ((FieldInfo)member).FieldType; break;

            case MemberTypes.Property:
                effectiveType = ((PropertyInfo)member).PropertyType; break;

            default:
                return(null);    // nothing doing
            }

            int    fieldNumber = 0;
            bool   isPacked    = false;
            string name        = null;
            bool   isRequired  = false;
            Type   itemType    = null;
            Type   defaultType = null;

            ResolveListTypes(effectiveType, ref itemType, ref defaultType);
            object[]   attribs = member.GetCustomAttributes(true);
            Attribute  attrib;
            DataFormat dataFormat   = DataFormat.Default;
            bool       ignore       = false;
            object     defaultValue = null;

            // implicit zero default
            switch (Type.GetTypeCode(effectiveType))
            {
            case TypeCode.Boolean: defaultValue = false; break;

            case TypeCode.Decimal: defaultValue = (decimal)0; break;

            case TypeCode.Single: defaultValue = (float)0; break;

            case TypeCode.Double: defaultValue = (double)0; break;

            case TypeCode.Byte: defaultValue = (byte)0;  break;

            case TypeCode.Char: defaultValue = (char)0; break;

            case TypeCode.Int16: defaultValue = (short)0; break;

            case TypeCode.Int32: defaultValue = (int)0; break;

            case TypeCode.Int64: defaultValue = (long)0; break;

            case TypeCode.SByte: defaultValue = (sbyte)0; break;

            case TypeCode.UInt16: defaultValue = (ushort)0; break;

            case TypeCode.UInt32: defaultValue = (uint)0; break;

            case TypeCode.UInt64: defaultValue = (ulong)0; break;

            default:
                if (effectiveType == typeof(TimeSpan))
                {
                    defaultValue = TimeSpan.Zero;
                }
                if (effectiveType == typeof(Guid))
                {
                    defaultValue = Guid.Empty;
                }
                break;
            }
            bool done = false;

            if (isEnum)
            {
                attrib = GetAttribute(attribs, "ProtoBuf.ProtoIgnoreAttribute");
                if (attrib != null)
                {
                    ignore = true;
                }
                else
                {
                    attrib      = GetAttribute(attribs, "ProtoBuf.ProtoEnumAttribute");
                    fieldNumber = Convert.ToInt32(((FieldInfo)member).GetValue(null));
                    if (attrib != null)
                    {
                        GetFieldName(ref name, attrib, "Name");
                        if ((bool)attrib.GetType().GetMethod("HasValue").Invoke(attrib, null))
                        {
                            fieldNumber = (int)GetMemberValue(attrib, "Value");
                        }
                    }
                }
                done = true;
            }
            if (!ignore && !done) // always consider ProtoMember
            {
                attrib = GetAttribute(attribs, "ProtoBuf.ProtoMemberAttribute");
                GetIgnore(ref ignore, attrib, attribs, "ProtoBuf.ProtoIgnoreAttribute");
                if (!ignore)
                {
                    GetFieldNumber(ref fieldNumber, attrib, "Tag");
                    GetFieldName(ref name, attrib, "Name");
                    GetFieldBoolean(ref isRequired, attrib, "IsRequired");
                    GetFieldBoolean(ref isPacked, attrib, "IsPacked");
                    GetDataFormat(ref dataFormat, attrib, "DataFormat");
                    done = fieldNumber > 0;
                }

                if (!done && partialMembers != null)
                {
                    foreach (ProtoPartialMemberAttribute ppma in partialMembers)
                    {
                        if (ppma.MemberName == member.Name)
                        {
                            GetFieldNumber(ref fieldNumber, ppma, "Tag");
                            GetFieldName(ref name, ppma, "Name");
                            GetFieldBoolean(ref isRequired, ppma, "IsRequired");
                            GetFieldBoolean(ref isPacked, ppma, "IsPacked");
                            GetDataFormat(ref dataFormat, ppma, "DataFormat");
                            if (done = fieldNumber > 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            if (!ignore && !done && HasFamily(family, AttributeFamily.DataContractSerialier))
            {
                attrib = GetAttribute(attribs, "System.Runtime.Serialization.DataMemberAttribute");
                GetFieldNumber(ref fieldNumber, attrib, "Order");
                GetFieldName(ref name, attrib, "Name");
                GetFieldBoolean(ref isRequired, attrib, "IsRequired");
                done = fieldNumber > 0;
            }
            if (!ignore && !done && HasFamily(family, AttributeFamily.XmlSerializer))
            {
                attrib = GetAttribute(attribs, "System.Xml.Serialization.XmlElementAttribute");
                GetIgnore(ref ignore, attrib, attribs, "System.Xml.Serialization.XmlIgnoreAttribute");
                if (!ignore)
                {
                    GetFieldNumber(ref fieldNumber, attrib, "Order");
                    GetFieldName(ref name, attrib, "ElementName");
                }
                attrib = GetAttribute(attribs, "System.Xml.Serialization.XmlArrayAttribute");
                GetIgnore(ref ignore, attrib, attribs, "ProtoBuf.XmlIgnoreAttribute");
                if (!ignore)
                {
                    GetFieldNumber(ref fieldNumber, attrib, "Order");
                    GetFieldName(ref name, attrib, "ElementName");
                }
                done = fieldNumber > 0;
            }
            if (!ignore && (attrib = GetAttribute(attribs, "System.ComponentModel.DefaultValueAttribute")) != null)
            {
                defaultValue = GetMemberValue(attrib, "Value");
            }
            ValueMember vm = ((isEnum || fieldNumber > 0) && !ignore)
                ? new ValueMember(model, type, fieldNumber, member, effectiveType, itemType, defaultType, dataFormat, defaultValue)
                    : null;

            if (vm != null)
            {
                PropertyInfo prop = type.GetProperty(member.Name + "Specified", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                     null, typeof(bool), Helpers.EmptyTypes, null);
                if (prop != null)
                {
                    vm.SetSpecified(prop.GetGetMethod(true), prop.GetSetMethod(true));
                }
                else
                {
                    MethodInfo method = type.GetMethod("ShouldSerialize" + member.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                       null, Helpers.EmptyTypes, null);
                    if (method != null && method.ReturnType == typeof(bool))
                    {
                        vm.SetSpecified(method, null);
                    }
                }
                if (!Helpers.IsNullOrEmpty(name))
                {
                    vm.SetName(name);
                }
                vm.IsPacked   = isPacked;
                vm.IsRequired = isRequired;
            }
            return(vm);
        }
Beispiel #10
0
        internal void ApplyDefaultBehaviour()
        {
            if (model.FindWithoutAdd(type.BaseType) == null &&
                GetContractFamily(type.BaseType, null) != MetaType.AttributeFamily.None)
            {
                model.FindOrAddAuto(type.BaseType, true, false, false);
            }

            object[]        typeAttribs = type.GetCustomAttributes(true);
            AttributeFamily family      = GetContractFamily(type, typeAttribs);
            bool            isEnum      = type.IsEnum;

            if (family == AttributeFamily.None && !isEnum)
            {
                return;                                            // and you'd like me to do what, exactly?
            }
            BasicList partialIgnores = null, partialMembers = null;

            for (int i = 0; i < typeAttribs.Length; i++)
            {
                if (!isEnum && typeAttribs[i] is ProtoIncludeAttribute)
                {
                    ProtoIncludeAttribute pia = (ProtoIncludeAttribute)typeAttribs[i];
                    AddSubType(pia.Tag, pia.KnownType);
                }
                if (typeAttribs[i] is ProtoPartialIgnoreAttribute)
                {
                    if (partialIgnores == null)
                    {
                        partialIgnores = new BasicList();
                    }
                    partialIgnores.Add(((ProtoPartialIgnoreAttribute)typeAttribs[i]).MemberName);
                }
                if (!isEnum && typeAttribs[i] is ProtoPartialMemberAttribute)
                {
                    if (partialMembers == null)
                    {
                        partialMembers = new BasicList();
                    }
                    partialMembers.Add(typeAttribs[i]);
                }
            }
            MethodInfo[] callbacks = null;
            foreach (MemberInfo member in type.GetMembers(isEnum ? BindingFlags.Public | BindingFlags.Static
                : BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (member.DeclaringType != type)
                {
                    continue;
                }
                if (member.IsDefined(typeof(ProtoIgnoreAttribute), true))
                {
                    continue;
                }
                if (partialIgnores != null && partialIgnores.Contains(member.Name))
                {
                    continue;
                }

                switch (member.MemberType)
                {
                case MemberTypes.Property:
                case MemberTypes.Field:
                    ValueMember vm = ApplyDefaultBehaviour(isEnum, family, member, partialMembers);
                    if (vm != null)
                    {
                        Add(vm);
                    }
                    break;

                case MemberTypes.Method:
                    if (isEnum)
                    {
                        continue;
                    }
                    MethodInfo method        = (MethodInfo)member;
                    object[]   memberAttribs = Attribute.GetCustomAttributes(method);
                    if (memberAttribs != null && memberAttribs.Length > 0)
                    {
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeSerializationAttribute", ref callbacks, 0);
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterSerializationAttribute", ref callbacks, 1);
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeDeserializationAttribute", ref callbacks, 2);
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterDeserializationAttribute", ref callbacks, 3);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializingAttribute", ref callbacks, 4);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializedAttribute", ref callbacks, 5);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializingAttribute", ref callbacks, 6);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializedAttribute", ref callbacks, 7);
                    }
                    break;
                }
            }
            if (callbacks != null)
            {
                SetCallbacks(callbacks[0] ?? callbacks[4], callbacks[1] ?? callbacks[5],
                             callbacks[2] ?? callbacks[6], callbacks[3] ?? callbacks[7]);
            }
        }
        // Token: 0x06000389 RID: 905 RVA: 0x00013428 File Offset: 0x00011628
        internal string GetSchemaTypeName(Type effectiveType, DataFormat dataFormat, bool asReference, bool dynamicType, ref bool requiresBclImport)
        {
            Type underlyingType = Helpers.GetUnderlyingType(effectiveType);

            if (underlyingType != null)
            {
                effectiveType = underlyingType;
            }
            if (effectiveType == base.MapType(typeof(byte[])))
            {
                return("bytes");
            }
            WireType         wireType;
            IProtoSerializer protoSerializer = ValueMember.TryGetCoreSerializer(this, dataFormat, effectiveType, out wireType, false, false, false, false);

            if (protoSerializer == null)
            {
                if (asReference || dynamicType)
                {
                    requiresBclImport = true;
                    return("bcl.NetObjectProxy");
                }
                return(this[effectiveType].GetSurrogateOrBaseOrSelf(true).GetSchemaTypeName());
            }
            else
            {
                if (!(protoSerializer is ParseableSerializer))
                {
                    ProtoTypeCode typeCode = Helpers.GetTypeCode(effectiveType);
                    switch (typeCode)
                    {
                    case ProtoTypeCode.Boolean:
                        return("bool");

                    case ProtoTypeCode.Char:
                    case ProtoTypeCode.Byte:
                    case ProtoTypeCode.UInt16:
                    case ProtoTypeCode.UInt32:
                        if (dataFormat == DataFormat.FixedSize)
                        {
                            return("fixed32");
                        }
                        return("uint32");

                    case ProtoTypeCode.SByte:
                    case ProtoTypeCode.Int16:
                    case ProtoTypeCode.Int32:
                        if (dataFormat == DataFormat.ZigZag)
                        {
                            return("sint32");
                        }
                        if (dataFormat != DataFormat.FixedSize)
                        {
                            return("int32");
                        }
                        return("sfixed32");

                    case ProtoTypeCode.Int64:
                        if (dataFormat == DataFormat.ZigZag)
                        {
                            return("sint64");
                        }
                        if (dataFormat != DataFormat.FixedSize)
                        {
                            return("int64");
                        }
                        return("sfixed64");

                    case ProtoTypeCode.UInt64:
                        if (dataFormat == DataFormat.FixedSize)
                        {
                            return("fixed64");
                        }
                        return("uint64");

                    case ProtoTypeCode.Single:
                        return("float");

                    case ProtoTypeCode.Double:
                        return("double");

                    case ProtoTypeCode.Decimal:
                        requiresBclImport = true;
                        return("bcl.Decimal");

                    case ProtoTypeCode.DateTime:
                        requiresBclImport = true;
                        return("bcl.DateTime");

                    case (ProtoTypeCode)17:
                        break;

                    case ProtoTypeCode.String:
                        if (asReference)
                        {
                            requiresBclImport = true;
                        }
                        if (!asReference)
                        {
                            return("string");
                        }
                        return("bcl.NetObjectProxy");

                    default:
                        if (typeCode == ProtoTypeCode.TimeSpan)
                        {
                            requiresBclImport = true;
                            return("bcl.TimeSpan");
                        }
                        if (typeCode == ProtoTypeCode.Guid)
                        {
                            requiresBclImport = true;
                            return("bcl.Guid");
                        }
                        break;
                    }
                    throw new NotSupportedException("No .proto map found for: " + effectiveType.FullName);
                }
                if (asReference)
                {
                    requiresBclImport = true;
                }
                if (!asReference)
                {
                    return("string");
                }
                return("bcl.NetObjectProxy");
            }
        }
 // Token: 0x0600036A RID: 874 RVA: 0x00012A1C File Offset: 0x00010C1C
 private void CascadeDependents(BasicList list, MetaType metaType)
 {
     if (metaType.IsList)
     {
         Type     listItemType = TypeModel.GetListItemType(this, metaType.Type);
         WireType wireType;
         if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, listItemType, out wireType, false, false, false, false) == null)
         {
             int num = this.FindOrAddAuto(listItemType, false, false, false);
             if (num >= 0)
             {
                 MetaType metaType2 = ((MetaType)this.types[num]).GetSurrogateOrBaseOrSelf(false);
                 if (!list.Contains(metaType2))
                 {
                     list.Add(metaType2);
                     this.CascadeDependents(list, metaType2);
                     return;
                 }
             }
         }
     }
     else
     {
         MetaType metaType2;
         if (metaType.IsAutoTuple)
         {
             MemberInfo[] array;
             if (MetaType.ResolveTupleConstructor(metaType.Type, out array) != null)
             {
                 for (int i = 0; i < array.Length; i++)
                 {
                     Type type = null;
                     if (array[i] is PropertyInfo)
                     {
                         type = ((PropertyInfo)array[i]).PropertyType;
                     }
                     else if (array[i] is FieldInfo)
                     {
                         type = ((FieldInfo)array[i]).FieldType;
                     }
                     WireType wireType2;
                     if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out wireType2, false, false, false, false) == null)
                     {
                         int num2 = this.FindOrAddAuto(type, false, false, false);
                         if (num2 >= 0)
                         {
                             metaType2 = ((MetaType)this.types[num2]).GetSurrogateOrBaseOrSelf(false);
                             if (!list.Contains(metaType2))
                             {
                                 list.Add(metaType2);
                                 this.CascadeDependents(list, metaType2);
                             }
                         }
                     }
                 }
             }
         }
         else
         {
             foreach (object obj in metaType.Fields)
             {
                 ValueMember valueMember = (ValueMember)obj;
                 Type        type2       = valueMember.ItemType;
                 if (type2 == null)
                 {
                     type2 = valueMember.MemberType;
                 }
                 WireType wireType3;
                 if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type2, out wireType3, false, false, false, false) == null)
                 {
                     int num3 = this.FindOrAddAuto(type2, false, false, false);
                     if (num3 >= 0)
                     {
                         metaType2 = ((MetaType)this.types[num3]).GetSurrogateOrBaseOrSelf(false);
                         if (!list.Contains(metaType2))
                         {
                             list.Add(metaType2);
                             this.CascadeDependents(list, metaType2);
                         }
                     }
                 }
             }
         }
         if (metaType.HasSubtypes)
         {
             SubType[] subtypes = metaType.GetSubtypes();
             for (int j = 0; j < subtypes.Length; j++)
             {
                 metaType2 = subtypes[j].DerivedType.GetSurrogateOrSelf();
                 if (!list.Contains(metaType2))
                 {
                     list.Add(metaType2);
                     this.CascadeDependents(list, metaType2);
                 }
             }
         }
         metaType2 = metaType.BaseType;
         if (metaType2 != null)
         {
             metaType2 = metaType2.GetSurrogateOrSelf();
         }
         if (metaType2 != null && !list.Contains(metaType2))
         {
             list.Add(metaType2);
             this.CascadeDependents(list, metaType2);
         }
     }
 }
        // Token: 0x06000369 RID: 873 RVA: 0x00012780 File Offset: 0x00010980
        public override string GetSchema(Type type)
        {
            BasicList basicList = new BasicList();
            MetaType  metaType  = null;
            bool      flag      = false;

            if (type == null)
            {
                foreach (object obj in this.types)
                {
                    MetaType surrogateOrBaseOrSelf = ((MetaType)obj).GetSurrogateOrBaseOrSelf(false);
                    if (!basicList.Contains(surrogateOrBaseOrSelf))
                    {
                        basicList.Add(surrogateOrBaseOrSelf);
                        this.CascadeDependents(basicList, surrogateOrBaseOrSelf);
                    }
                }
            }
            else
            {
                Type underlyingType = Helpers.GetUnderlyingType(type);
                if (underlyingType != null)
                {
                    type = underlyingType;
                }
                WireType wireType;
                flag = (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out wireType, false, false, false, false) != null);
                if (!flag)
                {
                    int num = this.FindOrAddAuto(type, false, false, false);
                    if (num < 0)
                    {
                        throw new ArgumentException("The type specified is not a contract-type", "type");
                    }
                    metaType = ((MetaType)this.types[num]).GetSurrogateOrBaseOrSelf(false);
                    basicList.Add(metaType);
                    this.CascadeDependents(basicList, metaType);
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            string        text          = null;

            if (!flag)
            {
                foreach (object obj2 in ((IEnumerable)((metaType == null) ? this.types : basicList)))
                {
                    MetaType metaType2 = (MetaType)obj2;
                    if (!metaType2.IsList)
                    {
                        string @namespace = metaType2.Type.Namespace;
                        if (!Helpers.IsNullOrEmpty(@namespace) && [email protected]("System."))
                        {
                            if (text == null)
                            {
                                text = @namespace;
                            }
                            else if (!(text == @namespace))
                            {
                                text = null;
                                break;
                            }
                        }
                    }
                }
            }
            if (!Helpers.IsNullOrEmpty(text))
            {
                stringBuilder.Append("package ").Append(text).Append(';');
                Helpers.AppendLine(stringBuilder);
            }
            bool          flag2          = false;
            StringBuilder stringBuilder2 = new StringBuilder();

            MetaType[] array = new MetaType[basicList.Count];
            basicList.CopyTo(array, 0);
            Array.Sort <MetaType>(array, MetaType.Comparer.Default);
            if (flag)
            {
                Helpers.AppendLine(stringBuilder2).Append("message ").Append(type.Name).Append(" {");
                MetaType.NewLine(stringBuilder2, 1).Append("optional ").Append(this.GetSchemaTypeName(type, DataFormat.Default, false, false, ref flag2)).Append(" value = 1;");
                Helpers.AppendLine(stringBuilder2).Append('}');
            }
            else
            {
                foreach (MetaType metaType3 in array)
                {
                    if (!metaType3.IsList || metaType3 == metaType)
                    {
                        metaType3.WriteSchema(stringBuilder2, 0, ref flag2);
                    }
                }
            }
            if (flag2)
            {
                stringBuilder.Append("import \"bcl.proto\"; // schema for protobuf-net's handling of core .NET types");
                Helpers.AppendLine(stringBuilder);
            }
            return(Helpers.AppendLine(stringBuilder.Append(stringBuilder2)).ToString());
        }
 private void Add(ValueMember member) {
     lock (fields)
     {
         ThrowIfFrozen();
         fields.Add(member);
     }
 }
Beispiel #15
0
        public override string GetSchema(Type type)
        {
            BasicList basicList = new BasicList();
            MetaType  metaType  = null;
            bool      flag      = false;

            if (type == null)
            {
                BasicList.NodeEnumerator enumerator = this.types.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    MetaType metaType2             = (MetaType)enumerator.Current;
                    MetaType surrogateOrBaseOrSelf = metaType2.GetSurrogateOrBaseOrSelf(false);
                    if (!basicList.Contains(surrogateOrBaseOrSelf))
                    {
                        basicList.Add(surrogateOrBaseOrSelf);
                        this.CascadeDependents(basicList, surrogateOrBaseOrSelf);
                    }
                }
            }
            else
            {
                Type underlyingType = Helpers.GetUnderlyingType(type);
                if (underlyingType != null)
                {
                    type = underlyingType;
                }
                WireType wireType;
                flag = (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out wireType, false, false, false, false) != null);
                if (!flag)
                {
                    int num = this.FindOrAddAuto(type, false, false, false);
                    if (num < 0)
                    {
                        throw new ArgumentException("The type specified is not a contract-type", "type");
                    }
                    metaType = ((MetaType)this.types[num]).GetSurrogateOrBaseOrSelf(false);
                    basicList.Add(metaType);
                    this.CascadeDependents(basicList, metaType);
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            string        text          = null;

            if (!flag)
            {
                IEnumerable enumerable  = (metaType != null) ? basicList : this.types;
                IEnumerator enumerator2 = enumerable.GetEnumerator();
                try
                {
                    while (enumerator2.MoveNext())
                    {
                        MetaType metaType3 = (MetaType)enumerator2.get_Current();
                        if (!metaType3.IsList)
                        {
                            string @namespace = metaType3.Type.get_Namespace();
                            if (!Helpers.IsNullOrEmpty(@namespace))
                            {
                                if ([email protected]("System."))
                                {
                                    if (text == null)
                                    {
                                        text = @namespace;
                                    }
                                    else if (!(text == @namespace))
                                    {
                                        text = null;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator2 as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
            if (!Helpers.IsNullOrEmpty(text))
            {
                stringBuilder.Append("package ").Append(text).Append(';');
                Helpers.AppendLine(stringBuilder);
            }
            bool          flag2          = false;
            StringBuilder stringBuilder2 = new StringBuilder();

            MetaType[] array = new MetaType[basicList.Count];
            basicList.CopyTo(array, 0);
            Array.Sort <MetaType>(array, MetaType.Comparer.Default);
            if (flag)
            {
                Helpers.AppendLine(stringBuilder2).Append("message ").Append(type.get_Name()).Append(" {");
                MetaType.NewLine(stringBuilder2, 1).Append("optional ").Append(this.GetSchemaTypeName(type, DataFormat.Default, false, false, ref flag2)).Append(" value = 1;");
                Helpers.AppendLine(stringBuilder2).Append('}');
            }
            else
            {
                for (int i = 0; i < array.Length; i++)
                {
                    MetaType metaType4 = array[i];
                    if (!metaType4.IsList || metaType4 == metaType)
                    {
                        metaType4.WriteSchema(stringBuilder2, 0, ref flag2);
                    }
                }
            }
            if (flag2)
            {
                stringBuilder.Append("import \"bcl.proto\"; // schema for protobuf-net's handling of core .NET types");
                Helpers.AppendLine(stringBuilder);
            }
            return(Helpers.AppendLine(stringBuilder.Append(stringBuilder2)).ToString());
        }
Beispiel #16
0
        private IProtoTypeSerializer BuildSerializer()
        {
            if (type.IsEnum)
            {
                //return new TagDecorator(ProtoBuf.Serializer.ListItemTag, WireType.Variant, false, new EnumSerializer(type, GetEnumMap()));
                return new TypeSerializer(type, new int[] { ProtoBuf.Serializer.ListItemTag },
                    new IProtoSerializer[] {
                        new TagDecorator(ProtoBuf.Serializer.ListItemTag, WireType.Variant, false, new EnumSerializer(type, GetEnumMap()))
                    }, null, true, true, null, constructType);
            }
            if (surrogate != null)
            {
                MetaType mt = model[surrogate], mtBase;
                while ((mtBase = mt.baseType) != null) { mt = mtBase; }
                return new SurrogateSerializer(type, surrogate, mt.Serializer);
            }

            Type itemType = TypeModel.GetListItemType(type);
            if (itemType != null)
            {
                ValueMember fakeMember = new ValueMember(model, ProtoBuf.Serializer.ListItemTag, type, itemType, type, DataFormat.Default);
                return new TypeSerializer(type, new int[] { ProtoBuf.Serializer.ListItemTag }, new IProtoSerializer[] { fakeMember.Serializer }, null, true, true, null, constructType);
            }

            fields.Trim();
            int fieldCount = fields.Count;
            int subTypeCount = subTypes == null ? 0 : subTypes.Count;
            int[] fieldNumbers = new int[fieldCount + subTypeCount];
            IProtoSerializer[] serializers = new IProtoSerializer[fieldCount + subTypeCount];
            int i = 0;
            if (subTypeCount != 0)
            {
                foreach (SubType subType in subTypes)
                {
                    fieldNumbers[i] = subType.FieldNumber;
                    serializers[i++] = subType.Serializer;
                }
            }
            if (fieldCount != 0)
            {
                foreach (ValueMember member in fields)
                {
                    fieldNumbers[i] = member.FieldNumber;
                    serializers[i++] = member.Serializer;
                }
            }

            BasicList baseCtorCallbacks = null;
            MetaType tmp = BaseType;

            while (tmp != null)
            {
                MethodInfo method = tmp.HasCallbacks ? tmp.Callbacks.BeforeDeserialize : null;
                if (method != null)
                {
                    if (baseCtorCallbacks == null) baseCtorCallbacks = new BasicList();
                    baseCtorCallbacks.Add(method);
                }
                tmp = tmp.BaseType;
            }
            MethodInfo[] arr = null;
            if (baseCtorCallbacks != null)
            {
                arr = new MethodInfo[baseCtorCallbacks.Count];
                baseCtorCallbacks.CopyTo(arr, 0);
                Array.Reverse(arr);
            }
            return new TypeSerializer(type, fieldNumbers, serializers, arr, baseType == null, UseConstructor, callbacks, constructType);
        }
Beispiel #17
0
        private ValueMember ApplyDefaultBehaviour(bool isEnum, ProtoMemberAttribute normalizedAttribute)
        {
            MemberInfo member;

            if (normalizedAttribute == null || (member = normalizedAttribute.Member) == null)
            {
                return(null);                                                                              // nix
            }
            Type effectiveType;

            switch (member.MemberType)
            {
            case MemberTypes.Field:
                effectiveType = ((FieldInfo)member).FieldType; break;

            case MemberTypes.Property:
                effectiveType = ((PropertyInfo)member).PropertyType; break;

            default:
                return(null);    // nothing doing
            }


            Type itemType    = null;
            Type defaultType = null;

            ResolveListTypes(effectiveType, ref itemType, ref defaultType);
            object[]  attribs = member.GetCustomAttributes(true);
            Attribute attrib;

            object defaultValue = null;

            // implicit zero default
            switch (Type.GetTypeCode(effectiveType))
            {
            case TypeCode.Boolean: defaultValue = false; break;

            case TypeCode.Decimal: defaultValue = (decimal)0; break;

            case TypeCode.Single: defaultValue = (float)0; break;

            case TypeCode.Double: defaultValue = (double)0; break;

            case TypeCode.Byte: defaultValue = (byte)0;  break;

            case TypeCode.Char: defaultValue = (char)0; break;

            case TypeCode.Int16: defaultValue = (short)0; break;

            case TypeCode.Int32: defaultValue = (int)0; break;

            case TypeCode.Int64: defaultValue = (long)0; break;

            case TypeCode.SByte: defaultValue = (sbyte)0; break;

            case TypeCode.UInt16: defaultValue = (ushort)0; break;

            case TypeCode.UInt32: defaultValue = (uint)0; break;

            case TypeCode.UInt64: defaultValue = (ulong)0; break;

            default:
                if (effectiveType == typeof(TimeSpan))
                {
                    defaultValue = TimeSpan.Zero;
                }
                if (effectiveType == typeof(Guid))
                {
                    defaultValue = Guid.Empty;
                }
                break;
            }

            if ((attrib = GetAttribute(attribs, "System.ComponentModel.DefaultValueAttribute")) != null)
            {
                defaultValue = GetMemberValue(attrib, "Value");
            }
            ValueMember vm = ((isEnum || normalizedAttribute.Tag > 0))
                ? new ValueMember(model, type, normalizedAttribute.Tag, member, effectiveType, itemType, defaultType, normalizedAttribute.DataFormat, defaultValue)
                    : null;

            if (vm != null)
            {
                PropertyInfo prop = type.GetProperty(member.Name + "Specified", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                     null, typeof(bool), Helpers.EmptyTypes, null);
                if (prop != null)
                {
                    vm.SetSpecified(prop.GetGetMethod(true), prop.GetSetMethod(true));
                }
                else
                {
                    MethodInfo method = type.GetMethod("ShouldSerialize" + member.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                       null, Helpers.EmptyTypes, null);
                    if (method != null && method.ReturnType == typeof(bool))
                    {
                        vm.SetSpecified(method, null);
                    }
                }
                if (!Helpers.IsNullOrEmpty(normalizedAttribute.Name))
                {
                    vm.SetName(normalizedAttribute.Name);
                }
                vm.IsPacked      = normalizedAttribute.IsPacked;
                vm.IsRequired    = normalizedAttribute.IsRequired;
                vm.OverwriteList = normalizedAttribute.OverwriteList;
                vm.AsReference   = normalizedAttribute.AsReference;
                vm.DynamicType   = normalizedAttribute.DynamicType;
            }
            return(vm);
        }
Beispiel #18
0
        internal void ApplyDefaultBehaviour()
        {
            if (type.BaseType != null && model.FindWithoutAdd(type.BaseType) == null &&
                GetContractFamily(type.BaseType, null) != MetaType.AttributeFamily.None)
            {
                model.FindOrAddAuto(type.BaseType, true, false, false);
            }

            object[]        typeAttribs = type.GetCustomAttributes(true);
            AttributeFamily family      = GetContractFamily(type, typeAttribs);
            bool            isEnum      = type.IsEnum;

            if (family == AttributeFamily.None && !isEnum)
            {
                return;                                            // and you'd like me to do what, exactly?
            }
            BasicList      partialIgnores = null, partialMembers = null;
            int            dataMemberOffset = 0, implicitFirstTag = 1;
            bool           inferTagByName = model.InferTagFromNameDefault;
            ImplicitFields implicitMode   = ImplicitFields.None;

            for (int i = 0; i < typeAttribs.Length; i++)
            {
                Attribute item = (Attribute)typeAttribs[i];
                if (!isEnum && item is ProtoIncludeAttribute)
                {
                    ProtoIncludeAttribute pia = (ProtoIncludeAttribute)item;
                    AddSubType(pia.Tag, pia.KnownType);
                }
                if (item is ProtoPartialIgnoreAttribute)
                {
                    if (partialIgnores == null)
                    {
                        partialIgnores = new BasicList();
                    }
                    partialIgnores.Add(((ProtoPartialIgnoreAttribute)item).MemberName);
                }
                if (!isEnum && item is ProtoPartialMemberAttribute)
                {
                    if (partialMembers == null)
                    {
                        partialMembers = new BasicList();
                    }
                    partialMembers.Add(item);
                }
                if (!isEnum && item is ProtoContractAttribute)
                {
                    ProtoContractAttribute pca = (ProtoContractAttribute)item;
                    dataMemberOffset = pca.DataMemberOffset;
                    if (pca.InferTagFromNameHasValue)
                    {
                        inferTagByName = pca.InferTagFromName;
                    }
                    implicitMode   = pca.ImplicitFields;
                    UseConstructor = !pca.SkipConstructor;
                    if (pca.ImplicitFirstTag > 0)
                    {
                        implicitFirstTag = pca.ImplicitFirstTag;
                    }
                }
            }
            if (implicitMode != ImplicitFields.None)
            {
                family &= AttributeFamily.ProtoBuf; // with implicit fields, **only** proto attributes are important
            }
            MethodInfo[] callbacks = null;

            BasicList members = new BasicList();

            foreach (MemberInfo member in type.GetMembers(isEnum ? BindingFlags.Public | BindingFlags.Static
                : BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (member.DeclaringType != type)
                {
                    continue;
                }
                if (member.IsDefined(typeof(ProtoIgnoreAttribute), true))
                {
                    continue;
                }
                if (partialIgnores != null && partialIgnores.Contains(member.Name))
                {
                    continue;
                }

                bool forced = false, isPublic, isField;
                Type effectiveType;
                switch (member.MemberType)
                {
                case MemberTypes.Property:
                    PropertyInfo property = (PropertyInfo)member;
                    effectiveType = property.PropertyType;
                    isPublic      = property.GetGetMethod(false) != null;
                    isField       = false;
                    goto ProcessMember;

                case MemberTypes.Field:
                    FieldInfo field = (FieldInfo)member;
                    effectiveType = field.FieldType;
                    isPublic      = field.IsPublic;
                    isField       = true;
ProcessMember:
                    switch (implicitMode)
                    {
                    case ImplicitFields.AllFields:
                        if (isField)
                        {
                            forced = true;
                        }
                        break;

                    case ImplicitFields.AllPublic:
                        if (isPublic)
                        {
                            forced = true;
                        }
                        break;
                    }
                    if (effectiveType.IsSubclassOf(typeof(Delegate)))
                    {
                        continue;                                                   // we just don't like delegate types ;p
                    }
                    ProtoMemberAttribute normalizedAttribute = NormalizeProtoMember(member, family, forced, isEnum, partialMembers, dataMemberOffset, inferTagByName);
                    if (normalizedAttribute != null)
                    {
                        members.Add(normalizedAttribute);
                    }
                    break;

                case MemberTypes.Method:
                    if (isEnum)
                    {
                        continue;
                    }
                    MethodInfo method        = (MethodInfo)member;
                    object[]   memberAttribs = Attribute.GetCustomAttributes(method);
                    if (memberAttribs != null && memberAttribs.Length > 0)
                    {
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeSerializationAttribute", ref callbacks, 0);
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterSerializationAttribute", ref callbacks, 1);
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeDeserializationAttribute", ref callbacks, 2);
                        CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterDeserializationAttribute", ref callbacks, 3);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializingAttribute", ref callbacks, 4);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializedAttribute", ref callbacks, 5);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializingAttribute", ref callbacks, 6);
                        CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializedAttribute", ref callbacks, 7);
                    }
                    break;
                }
            }
            ProtoMemberAttribute[] arr = new ProtoMemberAttribute[members.Count];
            members.CopyTo(arr, 0);

            if (inferTagByName || implicitMode != ImplicitFields.None)
            {
                Array.Sort(arr);
                int nextTag = implicitFirstTag;
                foreach (ProtoMemberAttribute normalizedAttribute in arr)
                {
                    if (!normalizedAttribute.TagIsPinned) // if ProtoMember etc sets a tag, we'll trust it
                    {
                        normalizedAttribute.Rebase(nextTag++);
                    }
                }
            }

            foreach (ProtoMemberAttribute normalizedAttribute in arr)
            {
                ValueMember vm = ApplyDefaultBehaviour(isEnum, normalizedAttribute);
                if (vm != null)
                {
                    Add(vm);
                }
            }

            if (callbacks != null)
            {
                SetCallbacks(Coalesce(callbacks, 0, 4), Coalesce(callbacks, 1, 5),
                             Coalesce(callbacks, 2, 6), Coalesce(callbacks, 3, 7));
            }
        }
        public override string GetSchema(Type type)
        {
            BasicList basicList = new BasicList();
            MetaType  metaType  = null;
            bool      flag      = false;

            if (type == null)
            {
                BasicList.NodeEnumerator enumerator = types.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    MetaType surrogateOrBaseOrSelf = ((MetaType)enumerator.Current).GetSurrogateOrBaseOrSelf(deep: false);
                    if (!basicList.Contains(surrogateOrBaseOrSelf))
                    {
                        basicList.Add(surrogateOrBaseOrSelf);
                        CascadeDependents(basicList, surrogateOrBaseOrSelf);
                    }
                }
            }
            else
            {
                Type underlyingType = Helpers.GetUnderlyingType(type);
                if (underlyingType != null)
                {
                    type = underlyingType;
                }
                flag = (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out WireType _, asReference: false, dynamicType: false, overwriteList: false, allowComplexTypes: false) != null);
                if (!flag)
                {
                    int num = FindOrAddAuto(type, demand: false, addWithContractOnly: false, addEvenIfAutoDisabled: false);
                    if (num < 0)
                    {
                        throw new ArgumentException("The type specified is not a contract-type", "type");
                    }
                    metaType = ((MetaType)types[num]).GetSurrogateOrBaseOrSelf(deep: false);
                    basicList.Add(metaType);
                    CascadeDependents(basicList, metaType);
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            string        text          = null;

            if (!flag)
            {
                foreach (MetaType item in (IEnumerable)((metaType == null) ? types : basicList))
                {
                    if (!item.IsList)
                    {
                        string @namespace = item.Type.Namespace;
                        if (!Helpers.IsNullOrEmpty(@namespace) && [email protected]("System."))
                        {
                            if (text == null)
                            {
                                text = @namespace;
                            }
                            else if (!(text == @namespace))
                            {
                                text = null;
                                break;
                            }
                        }
                    }
                }
            }
            if (!Helpers.IsNullOrEmpty(text))
            {
                stringBuilder.Append("package ").Append(text).Append(';');
                Helpers.AppendLine(stringBuilder);
            }
            bool          requiresBclImport = false;
            StringBuilder stringBuilder2    = new StringBuilder();

            MetaType[] array = new MetaType[basicList.Count];
            basicList.CopyTo(array, 0);
            Array.Sort(array, MetaType.Comparer.Default);
            if (flag)
            {
                Helpers.AppendLine(stringBuilder2).Append("message ").Append(type.Name)
                .Append(" {");
                MetaType.NewLine(stringBuilder2, 1).Append("optional ").Append(GetSchemaTypeName(type, DataFormat.Default, asReference: false, dynamicType: false, ref requiresBclImport))
                .Append(" value = 1;");
                Helpers.AppendLine(stringBuilder2).Append('}');
            }
            else
            {
                foreach (MetaType metaType3 in array)
                {
                    if (!metaType3.IsList || metaType3 == metaType)
                    {
                        metaType3.WriteSchema(stringBuilder2, 0, ref requiresBclImport);
                    }
                }
            }
            if (requiresBclImport)
            {
                stringBuilder.Append("import \"bcl.proto\"; // schema for protobuf-net's handling of core .NET types");
                Helpers.AppendLine(stringBuilder);
            }
            return(Helpers.AppendLine(stringBuilder.Append(stringBuilder2)).ToString());
        }
Beispiel #20
0
        // Token: 0x0600040A RID: 1034 RVA: 0x00015168 File Offset: 0x00013368
        private IProtoSerializer BuildSerializer()
        {
            int opaqueToken = 0;
            IProtoSerializer result;

            try
            {
                this.model.TakeLock(ref opaqueToken);
                Type             type = (this.itemType == null) ? this.memberType : this.itemType;
                WireType         wireType;
                IProtoSerializer protoSerializer = ValueMember.TryGetCoreSerializer(this.model, this.dataFormat, type, out wireType, this.asReference, this.dynamicType, this.OverwriteList, true);
                if (protoSerializer == null)
                {
                    throw new InvalidOperationException("No serializer defined for type: " + type.FullName);
                }
                if (this.itemType != null && this.SupportNull)
                {
                    if (this.IsPacked)
                    {
                        throw new NotSupportedException("Packed encodings cannot support null values");
                    }
                    protoSerializer = new TagDecorator(1, wireType, this.IsStrict, protoSerializer);
                    protoSerializer = new NullDecorator(this.model, protoSerializer);
                    protoSerializer = new TagDecorator(this.fieldNumber, WireType.StartGroup, false, protoSerializer);
                }
                else
                {
                    protoSerializer = new TagDecorator(this.fieldNumber, wireType, this.IsStrict, protoSerializer);
                }
                if (this.itemType != null)
                {
                    if (!this.SupportNull)
                    {
                        if (Helpers.GetUnderlyingType(this.itemType) == null)
                        {
                            Type type2 = this.itemType;
                        }
                    }
                    else
                    {
                        Type type3 = this.itemType;
                    }
                    if (this.memberType.IsArray)
                    {
                        protoSerializer = new ArrayDecorator(this.model, protoSerializer, this.fieldNumber, this.IsPacked, wireType, this.memberType, this.OverwriteList, this.SupportNull);
                    }
                    else
                    {
                        protoSerializer = ListDecorator.Create(this.model, this.memberType, this.defaultType, protoSerializer, this.fieldNumber, this.IsPacked, wireType, this.member != null && PropertyDecorator.CanWrite(this.model, this.member), this.OverwriteList, this.SupportNull);
                    }
                }
                else if (this.defaultValue != null && !this.IsRequired && this.getSpecified == null)
                {
                    protoSerializer = new DefaultValueDecorator(this.model, this.defaultValue, protoSerializer);
                }
                if (this.memberType == this.model.MapType(typeof(Uri)))
                {
                    protoSerializer = new UriDecorator(this.model, protoSerializer);
                }
                if (this.member != null)
                {
                    if (this.member is PropertyInfo)
                    {
                        protoSerializer = new PropertyDecorator(this.model, this.parentType, (PropertyInfo)this.member, protoSerializer);
                    }
                    else
                    {
                        if (!(this.member is FieldInfo))
                        {
                            throw new InvalidOperationException();
                        }
                        protoSerializer = new FieldDecorator(this.parentType, (FieldInfo)this.member, protoSerializer);
                    }
                    if (this.getSpecified != null || this.setSpecified != null)
                    {
                        protoSerializer = new MemberSpecifiedDecorator(this.getSpecified, this.setSpecified, protoSerializer);
                    }
                }
                result = protoSerializer;
            }
            finally
            {
                this.model.ReleaseLock(opaqueToken);
            }
            return(result);
        }
Beispiel #21
0
 private void CascadeDependents(BasicList list, MetaType metaType)
 {
     if (metaType.IsList)
     {
         Type     listItemType = TypeModel.GetListItemType(this, metaType.Type);
         WireType wireType;
         if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, listItemType, out wireType, false, false, false, false) == null)
         {
             int num = this.FindOrAddAuto(listItemType, false, false, false);
             if (num >= 0)
             {
                 MetaType metaType2 = ((MetaType)this.types[num]).GetSurrogateOrBaseOrSelf(false);
                 if (!list.Contains(metaType2))
                 {
                     list.Add(metaType2);
                     this.CascadeDependents(list, metaType2);
                 }
             }
         }
     }
     else
     {
         MetaType metaType2;
         if (metaType.IsAutoTuple)
         {
             MemberInfo[] array;
             if (MetaType.ResolveTupleConstructor(metaType.Type, out array) != null)
             {
                 for (int i = 0; i < array.Length; i++)
                 {
                     Type type = null;
                     if (array[i] is PropertyInfo)
                     {
                         type = ((PropertyInfo)array[i]).get_PropertyType();
                     }
                     else if (array[i] is FieldInfo)
                     {
                         type = ((FieldInfo)array[i]).get_FieldType();
                     }
                     WireType wireType2;
                     if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out wireType2, false, false, false, false) == null)
                     {
                         int num2 = this.FindOrAddAuto(type, false, false, false);
                         if (num2 >= 0)
                         {
                             metaType2 = ((MetaType)this.types[num2]).GetSurrogateOrBaseOrSelf(false);
                             if (!list.Contains(metaType2))
                             {
                                 list.Add(metaType2);
                                 this.CascadeDependents(list, metaType2);
                             }
                         }
                     }
                 }
             }
         }
         else
         {
             IEnumerator enumerator = metaType.Fields.GetEnumerator();
             try
             {
                 while (enumerator.MoveNext())
                 {
                     ValueMember valueMember = (ValueMember)enumerator.get_Current();
                     Type        type2       = valueMember.ItemType;
                     if (type2 == null)
                     {
                         type2 = valueMember.MemberType;
                     }
                     WireType wireType3;
                     if (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type2, out wireType3, false, false, false, false) == null)
                     {
                         int num3 = this.FindOrAddAuto(type2, false, false, false);
                         if (num3 >= 0)
                         {
                             metaType2 = ((MetaType)this.types[num3]).GetSurrogateOrBaseOrSelf(false);
                             if (!list.Contains(metaType2))
                             {
                                 list.Add(metaType2);
                                 this.CascadeDependents(list, metaType2);
                             }
                         }
                     }
                 }
             }
             finally
             {
                 IDisposable disposable = enumerator as IDisposable;
                 if (disposable != null)
                 {
                     disposable.Dispose();
                 }
             }
         }
         if (metaType.HasSubtypes)
         {
             SubType[] subtypes = metaType.GetSubtypes();
             for (int j = 0; j < subtypes.Length; j++)
             {
                 SubType subType = subtypes[j];
                 metaType2 = subType.DerivedType.GetSurrogateOrSelf();
                 if (!list.Contains(metaType2))
                 {
                     list.Add(metaType2);
                     this.CascadeDependents(list, metaType2);
                 }
             }
         }
         metaType2 = metaType.BaseType;
         if (metaType2 != null)
         {
             metaType2 = metaType2.GetSurrogateOrSelf();
         }
         if (metaType2 != null && !list.Contains(metaType2))
         {
             list.Add(metaType2);
             this.CascadeDependents(list, metaType2);
         }
     }
 }
        internal static IProtoSerializer TryGetCoreSerializer(RuntimeTypeModel model, ProtoBuf.DataFormat dataFormat, Type type, out WireType defaultWireType, bool asReference, bool dynamicType, bool overwriteList, bool allowComplexTypes)
        {
            IProtoSerializer protoSerializer;
            Type             underlyingType = Helpers.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }
            if (Helpers.IsEnum(type))
            {
                if (!allowComplexTypes || model == null)
                {
                    defaultWireType = WireType.None;
                    return(null);
                }
                defaultWireType = WireType.Variant;
                return(new EnumSerializer(type, model.GetEnumMap(type)));
            }
            ProtoTypeCode typeCode = Helpers.GetTypeCode(type);

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

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

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

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

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

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

            case ProtoTypeCode.Int32:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new Int32Serializer(model));
            }

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

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

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

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

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

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

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

            case ProtoTypeCode.Unknown | ProtoTypeCode.DateTime:
            {
Label0:
                if (model.AllowParseableTypes)
                {
                    protoSerializer = ParseableSerializer.TryCreate(type, model);
                }
                else
                {
                    protoSerializer = null;
                }
                IProtoSerializer protoSerializer1 = protoSerializer;
                if (protoSerializer1 != null)
                {
                    defaultWireType = WireType.String;
                    return(protoSerializer1);
                }
                if (allowComplexTypes && model != null)
                {
                    int key = model.GetKey(type, false, true);
                    if (asReference || dynamicType)
                    {
                        defaultWireType = (dataFormat == ProtoBuf.DataFormat.Group ? WireType.StartGroup : WireType.String);
                        BclHelpers.NetObjectOptions netObjectOption = BclHelpers.NetObjectOptions.None;
                        if (asReference)
                        {
                            netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.AsReference));
                        }
                        if (dynamicType)
                        {
                            netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.DynamicType));
                        }
                        if (key >= 0)
                        {
                            if (asReference && Helpers.IsValueType(type))
                            {
                                string str = "AsReference cannot be used with value-types";
                                str = (type.Name != "KeyValuePair`2" ? string.Concat(str, ": ", type.FullName) : string.Concat(str, "; please see http://stackoverflow.com/q/14436606/"));
                                throw new InvalidOperationException(str);
                            }
                            MetaType item = model[type];
                            if (asReference && item.IsAutoTuple)
                            {
                                netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.LateSet));
                            }
                            if (item.UseConstructor)
                            {
                                netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.UseConstructor));
                            }
                        }
                        return(new NetObjectSerializer(model, type, key, netObjectOption));
                    }
                    if (key >= 0)
                    {
                        defaultWireType = (dataFormat == ProtoBuf.DataFormat.Group ? WireType.StartGroup : WireType.String);
                        return(new SubItemSerializer(type, key, model[type], true));
                    }
                }
                defaultWireType = WireType.None;
                return(null);
            }

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

            default:
            {
                switch (typeCode)
                {
                case ProtoTypeCode.TimeSpan:
                {
                    defaultWireType = ValueMember.GetDateTimeWireType(dataFormat);
                    return(new TimeSpanSerializer(model));
                }

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

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

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

                case ProtoTypeCode.Type:
                {
                    defaultWireType = WireType.String;
                    return(new SystemTypeSerializer(model));
                }

                default:
                {
                    goto Label0;
                }
                }
                break;
            }
            }
        }
Beispiel #23
0
        private ValueMember AddField(int fieldNumber, string memberName, Type itemType, Type defaultType, object defaultValue)
        {
            MemberInfo[] members = type.GetMember(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            if(members == null || members.Length != 1) throw new ArgumentException("Unable to determine member: " + memberName, "memberName");
            MemberInfo mi = members[0];
            Type miType;
            switch (mi.MemberType)
            {
                case MemberTypes.Field:
                    miType = ((FieldInfo)mi).FieldType; break;
                case MemberTypes.Property:
                    miType = ((PropertyInfo)mi).PropertyType; break;
                default:
                    throw new NotSupportedException(mi.MemberType.ToString());
            }

            ResolveListTypes(miType, ref itemType, ref defaultType);
            ValueMember newField = new ValueMember(model, type, fieldNumber, mi, miType, itemType, defaultType, DataFormat.Default, defaultValue);
            Add(newField);
            return newField;
        }
Beispiel #24
0
        private IProtoTypeSerializer BuildSerializer()
        {
            if (surrogate != null)
            {
                MetaType mt = model[surrogate], mtBase;
                while ((mtBase = mt.baseType) != null)
                {
                    mt = mtBase;
                }
                return(new SurrogateSerializer(type, surrogate, mt.Serializer));
            }

            Type itemType = TypeModel.GetListItemType(type);

            if (itemType != null)
            {
                ValueMember fakeMember = new ValueMember(model, 1, type, itemType, type, DataFormat.Default);
                return(new TypeSerializer(type, new int[] { 1 }, new IProtoSerializer[] { fakeMember.Serializer }, null, true, true, null, constructType));
            }

            fields.Trim();
            int fieldCount   = fields.Count;
            int subTypeCount = subTypes == null ? 0 : subTypes.Count;

            int[] fieldNumbers             = new int[fieldCount + subTypeCount];
            IProtoSerializer[] serializers = new IProtoSerializer[fieldCount + subTypeCount];
            int i = 0;

            if (subTypeCount != 0)
            {
                foreach (SubType subType in subTypes)
                {
                    fieldNumbers[i]  = subType.FieldNumber;
                    serializers[i++] = subType.Serializer;
                }
            }
            if (fieldCount != 0)
            {
                foreach (ValueMember member in fields)
                {
                    fieldNumbers[i]  = member.FieldNumber;
                    serializers[i++] = member.Serializer;
                }
            }

            BasicList baseCtorCallbacks = null;
            MetaType  tmp = BaseType;

            while (tmp != null)
            {
                MethodInfo method = tmp.HasCallbacks ? tmp.Callbacks.BeforeDeserialize : null;
                if (method != null)
                {
                    if (baseCtorCallbacks == null)
                    {
                        baseCtorCallbacks = new BasicList();
                    }
                    baseCtorCallbacks.Add(method);
                }
                tmp = tmp.BaseType;
            }
            MethodInfo[] arr = null;
            if (baseCtorCallbacks != null)
            {
                arr = new MethodInfo[baseCtorCallbacks.Count];
                baseCtorCallbacks.CopyTo(arr, 0);
                Array.Reverse(arr);
            }
            return(new TypeSerializer(type, fieldNumbers, serializers, arr, baseType == null, UseConstructor, callbacks, constructType));
        }
Beispiel #25
0
        // Token: 0x0600040D RID: 1037 RVA: 0x00015408 File Offset: 0x00013608
        internal static IProtoSerializer TryGetCoreSerializer(RuntimeTypeModel model, DataFormat dataFormat, Type type, out WireType defaultWireType, bool asReference, bool dynamicType, bool overwriteList, bool allowComplexTypes)
        {
            Type underlyingType = Helpers.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }
            if (Helpers.IsEnum(type))
            {
                if (allowComplexTypes && model != null)
                {
                    defaultWireType = WireType.Variant;
                    return(new EnumSerializer(type, model.GetEnumMap(type)));
                }
                defaultWireType = WireType.None;
                return(null);
            }
            else
            {
                ProtoTypeCode typeCode = Helpers.GetTypeCode(type);
                switch (typeCode)
                {
                case ProtoTypeCode.Boolean:
                    defaultWireType = WireType.Variant;
                    return(new BooleanSerializer(model));

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

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

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

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

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

                case ProtoTypeCode.Int32:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new Int32Serializer(model));

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

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

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

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

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

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

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

                case (ProtoTypeCode)17:
                    break;

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

                default:
                    switch (typeCode)
                    {
                    case ProtoTypeCode.TimeSpan:
                        defaultWireType = ValueMember.GetDateTimeWireType(dataFormat);
                        return(new TimeSpanSerializer(model));

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

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

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

                    case ProtoTypeCode.Type:
                        defaultWireType = WireType.String;
                        return(new SystemTypeSerializer(model));
                    }
                    break;
                }
                IProtoSerializer protoSerializer = model.AllowParseableTypes ? ParseableSerializer.TryCreate(type, model) : null;
                if (protoSerializer != null)
                {
                    defaultWireType = WireType.String;
                    return(protoSerializer);
                }
                if (allowComplexTypes && model != null)
                {
                    int key = model.GetKey(type, false, true);
                    if (asReference || dynamicType)
                    {
                        defaultWireType = ((dataFormat == DataFormat.Group) ? WireType.StartGroup : WireType.String);
                        BclHelpers.NetObjectOptions netObjectOptions = BclHelpers.NetObjectOptions.None;
                        if (asReference)
                        {
                            netObjectOptions |= BclHelpers.NetObjectOptions.AsReference;
                        }
                        if (dynamicType)
                        {
                            netObjectOptions |= BclHelpers.NetObjectOptions.DynamicType;
                        }
                        if (key >= 0)
                        {
                            if (asReference && Helpers.IsValueType(type))
                            {
                                string text = "AsReference cannot be used with value-types";
                                if (type.Name == "KeyValuePair`2")
                                {
                                    text += "; please see http://stackoverflow.com/q/14436606/";
                                }
                                else
                                {
                                    text = text + ": " + type.FullName;
                                }
                                throw new InvalidOperationException(text);
                            }
                            MetaType metaType = model[type];
                            if (asReference && metaType.IsAutoTuple)
                            {
                                netObjectOptions |= BclHelpers.NetObjectOptions.LateSet;
                            }
                            if (metaType.UseConstructor)
                            {
                                netObjectOptions |= BclHelpers.NetObjectOptions.UseConstructor;
                            }
                        }
                        return(new NetObjectSerializer(model, type, key, netObjectOptions));
                    }
                    if (key >= 0)
                    {
                        defaultWireType = ((dataFormat == DataFormat.Group) ? WireType.StartGroup : WireType.String);
                        return(new SubItemSerializer(type, key, model[type], true));
                    }
                }
                defaultWireType = WireType.None;
                return(null);
            }
        }