Пример #1
0
        private static void ReadRootObjectMembersCount(ILGenerator il, Type type, SharedVariables variableCache)
        {
            if (type.IsExplicitStruct())
            {
                // member count is not written for structs
                return;
            }

            var notNull = il.DefineLabel();
            // CODE-FOR: if (!NumericSerializers.ReadVarUInt32Nullable(reader).HasValue)

            var readCountMethod = typeof(NumericSerializers).GetMethod(nameof(NumericSerializers.ReadVarUInt32Nullable),
                                                                       BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, Type.DefaultBinder,
                                                                       new[] { typeof(BinaryReader) }, null);

            var memberCount_shared = variableCache.GetOrAdd(typeof(uint?));

            il.Emit(OpCodes.Ldarg_0);             // BinaryReader
            il.Emit(OpCodes.Call, readCountMethod);
            il.StoreLocal(memberCount_shared);

            il.LoadLocalAddress(memberCount_shared);
            // ReSharper disable once PossibleNullReferenceException
            il.Emit(OpCodes.Call, typeof(uint?).GetProperty(nameof(Nullable <uint> .HasValue)).GetGetMethod());
            il.Emit(OpCodes.Brtrue_S, notNull);


            // CODE-FOR: return default(struct);
            // CODE-FOR: return null;
            if (type.IsExplicitStruct())
            {
                var defaultInstance = il.DeclareLocal(type);
                il.LoadLocalAddress(defaultInstance);
                il.Emit(OpCodes.Initobj, type);
                il.LoadLocalValue(defaultInstance);
                il.Emit(OpCodes.Ret);
            }
            else
            {
                il.Emit(OpCodes.Ldnull);
                il.Emit(OpCodes.Ret);
            }

            il.MarkLabel(notNull);


            variableCache.ReturnVariable(memberCount_shared);
        }
Пример #2
0
        private static void ReadRootObject(ILGenerator il, Type type, BoisComplexTypeInfo typeInfo)
        {
            if (typeInfo.Members == null || typeInfo.Members.Length == 0)
            {
                // no mmeber
                return;
            }

            var variableCache = new SharedVariables(il);

            ReadRootObjectMembersCount(il, type, variableCache);

            foreach (var member in typeInfo.Members)
            {
                ReadRootObjectMember(member, type, il, variableCache);
            }
        }
Пример #3
0
        private static void ReadRootObjectBasicMember(Type memberType, BoisBasicTypeInfo basicInfo, PropertyInfo prop, FieldInfo field, Type containerType, ILGenerator il, SharedVariables variableCache)
        {
            switch (basicInfo.KnownType)
            {
            case EnBasicKnownType.String:
                EmitGenerator.ReadString(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Bool:
                EmitGenerator.ReadBool(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Int16:
                EmitGenerator.ReadInt16(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Int32:
                EmitGenerator.ReadInt32(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Int64:
                EmitGenerator.ReadInt64(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.UInt16:
                EmitGenerator.ReadUInt16(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.UInt32:
                EmitGenerator.ReadUInt32(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.UInt64:
                EmitGenerator.ReadUInt64(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Double:
                EmitGenerator.ReadDouble(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Decimal:
                EmitGenerator.ReadDecimal(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Single:
                EmitGenerator.ReadFloat(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Byte:
                EmitGenerator.ReadByte(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.SByte:
                EmitGenerator.ReadSByte(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.DateTime:
                EmitGenerator.ReadDateTime(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.DateTimeOffset:
                EmitGenerator.ReadDateTimeOffset(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.KnownTypeArray:
                EmitGenerator.ReadUnknownArray(prop, field, null, containerType, il, basicInfo.IsNullable, variableCache);
                break;

            case EnBasicKnownType.ByteArray:
                EmitGenerator.ReadByteArray(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Enum:
                EmitGenerator.ReadEnum(prop, field, null, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.TimeSpan:
                EmitGenerator.ReadTimeSpan(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Char:
                EmitGenerator.ReadChar(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Guid:
                EmitGenerator.ReadGuid(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Color:
                EmitGenerator.ReadColor(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.DbNull:
                EmitGenerator.ReadDbNull(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Uri:
                EmitGenerator.ReadUri(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.Version:
                EmitGenerator.ReadVersion(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.DataTable:
                EmitGenerator.ReadDataTable(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            case EnBasicKnownType.DataSet:
                EmitGenerator.ReadDataSet(prop, field, null, containerType, il, basicInfo.IsNullable);
                break;

            default:
            case EnBasicKnownType.Unknown:
                return;
            }
        }
Пример #4
0
        private static void ReadRootObjectComplexMember(Type memberType, BoisComplexTypeInfo complexTypeInfo, PropertyInfo prop, FieldInfo field, Type containerType, ILGenerator il, SharedVariables variableCache)
        {
            switch (complexTypeInfo.ComplexKnownType)
            {
            case EnComplexKnownType.Collection:
            case EnComplexKnownType.ISet:
                EmitGenerator.ReadGenericCollection(prop, field, null, containerType, il, complexTypeInfo.IsNullable, variableCache);
                break;

            case EnComplexKnownType.Dictionary:
                EmitGenerator.ReadDictionary(prop, field, null, containerType, il, complexTypeInfo.IsNullable, variableCache);
                break;

            case EnComplexKnownType.UnknownArray:
                EmitGenerator.ReadUnknownArray(prop, field, null, containerType, il, complexTypeInfo.IsNullable, variableCache);
                break;

            case EnComplexKnownType.NameValueColl:
                EmitGenerator.ReadNameValueColl(prop, field, null, containerType, il, complexTypeInfo.IsNullable, variableCache);
                break;

            case EnComplexKnownType.Unknown:
            default:
                EmitGenerator.ReadUnknownComplexTypeCall(memberType, prop, field, containerType, il, complexTypeInfo);
                return;
            }
        }
Пример #5
0
        private static void ReadRootObjectMember(MemberInfo member, Type containerType, ILGenerator il, SharedVariables variableCache)
        {
            var prop  = member as PropertyInfo;
            var field = member as FieldInfo;

            Type memberType;

            if (prop != null)
            {
                memberType = prop.PropertyType;
            }
            else if (field != null)
            {
                memberType = field.FieldType;
            }
            else
            {
                return;
            }

            var basicInfo = BoisTypeCache.GetBasicType(memberType);

            if (basicInfo.KnownType != EnBasicKnownType.Unknown)
            {
                ReadRootObjectBasicMember(memberType, basicInfo, prop, field, containerType, il, variableCache);
            }
            else
            {
                var complexTypeInfo = BoisTypeCache.GetComplexTypeUnCached(memberType);
                ReadRootObjectComplexMember(memberType, complexTypeInfo, prop, field, containerType, il, variableCache);
            }
        }