Example #1
0
        /// <summary>
        /// 创建二进制数据反序列化
        /// </summary>
        /// <param name="attribute"></param>
        /// <returns></returns>
        internal TypeDeSerializer <valueType> CreateOnly(SerializeAttribute attribute)
        {
            DeSerializeVersionField[] fieldArray = this.fields;
            LeftArray <FieldSize>     fixedFields = new LeftArray <FieldSize>(fieldArray.Length - noSerializeMemberCount), fields = new LeftArray <FieldSize>(fieldArray.Length - noSerializeMemberCount);
            LeftArray <AutoCSer.Metadata.FieldIndex> jsonFields = new LeftArray <AutoCSer.Metadata.FieldIndex>();
            int fixedSize = 0;

            AutoCSer.Algorithm.QuickSort.Sort(fieldArray, DeSerializeVersionField.MemberNameSort, 0, fieldArray.Length);
            if (attribute.GetIsMemberMap)
            {
                int memberIndex = 0;
                LeftArray <DeSerializeVersionField> newFields = new LeftArray <DeSerializeVersionField>(fieldArray.Length);
                foreach (DeSerializeVersionField field in fieldArray)
                {
                    if (field.Attribute != null)
                    {
                        if (field.Field.MemberIndex == memberIndex)
                        {
                            newFields.Add(field);
                        }
                        else
                        {
                            newFields.Add(field.Copy(memberIndex));
                        }
                    }
                    ++memberIndex;
                }
                fieldArray = newFields.ToArray();
            }
            else if (noSerializeMemberCount != 0)
            {
                fieldArray = fieldArray.getFindArray(value => value.Attribute != null);
            }
            foreach (DeSerializeVersionField field in fieldArray)
            {
                SerializeMemberAttribute memberAttribute = field.Attribute;
                if (memberAttribute.GetIsJson)
                {
                    jsonFields.Add(field.Field);
                }
                else
                {
                    FieldSize value = new FieldSize(field.Field);
                    if (value.FixedSize == 0)
                    {
                        fields.Add(value);
                    }
                    else
                    {
                        fixedFields.Add(value);
                        fixedSize += value.FixedSize;
                    }
                }
            }
            int memberCountVerify;
            Fields <FieldSize> fieldSizes = new Fields <FieldSize>(ref fixedFields, ref fields, ref jsonFields, fixedSize, attribute.GetIsJson, out memberCountVerify);

            return(DeSerializer = new TypeDeSerializer <valueType>(GlobalVersion, ref fieldSizes, memberCountVerify));
        }
Example #2
0
 /// <summary>
 /// 创建二进制数据反序列化
 /// </summary>
 /// <param name="attribute"></param>
 /// <returns></returns>
 internal TypeDeSerializer <valueType> Create(SerializeAttribute attribute)
 {
     Monitor.Enter(CreateLock);
     try
     {
         if (DeSerializer == null)
         {
             CreateOnly(attribute);
             fields = null;
         }
     }
     finally { Monitor.Exit(CreateLock); }
     return(DeSerializer);
 }
Example #3
0
        static TypeSerializer()
        {
            Type       type = typeof(valueType), attributeType;
            MethodInfo methodInfo = Serializer.GetSerializeMethod(type);

            attribute = type.customAttribute <SerializeAttribute>(out attributeType) ?? Serializer.DefaultAttribute;
            if (methodInfo != null)
            {
                DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                isValueType       = true;
                IsReferenceMember = false;
                return;
            }
            if (type.IsArray)
            {
                isValueType = true;
                if (type.GetArrayRank() == 1)
                {
                    Type elementType = type.GetElementType();
                    if (!elementType.IsPointer && !typeof(Delegate).IsAssignableFrom(elementType))
                    {
                        if (elementType.IsValueType)
                        {
                            if (elementType.IsEnum)
                            {
                                Type enumType = System.Enum.GetUnderlyingType(elementType);
                                if (enumType == typeof(uint))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumUIntArrayMethod;                          // SerializeMethodCache.EnumUIntArrayMethod;
                                }
                                else if (enumType == typeof(byte))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumByteArrayMethod;                               //SerializeMethodCache.EnumByteArrayMethod;
                                }
                                else if (enumType == typeof(ulong))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumULongArrayMethod;                                //SerializeMethodCache.EnumULongArrayMethod;
                                }
                                else if (enumType == typeof(ushort))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumUShortArrayMethod;                                 //SerializeMethodCache.EnumUShortArrayMethod;
                                }
                                else if (enumType == typeof(long))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumLongArrayMethod;                               //SerializeMethodCache.EnumLongArrayMethod;
                                }
                                else if (enumType == typeof(short))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumShortArrayMethod;                                //SerializeMethodCache.EnumShortArrayMethod;
                                }
                                else if (enumType == typeof(sbyte))
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumSByteArrayMethod;                                //SerializeMethodCache.EnumSByteArrayMethod;
                                }
                                else
                                {
                                    methodInfo = GenericType.Get(elementType).BinarySerializeEnumIntArrayMethod; //SerializeMethodCache.EnumIntArrayMethod;
                                }
                                //methodInfo = methodInfo.MakeGenericMethod(elementType);
                                IsReferenceMember = false;
                            }
                            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                //methodInfo = SerializeMethodCache.NullableArrayMethod.MakeGenericMethod(elementType = elementType.GetGenericArguments()[0]);
                                methodInfo        = StructGenericType.Get(elementType = elementType.GetGenericArguments()[0]).BinarySerializeNullableArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                            else
                            {
                                //methodInfo = SerializeMethodCache.StructArrayMethod.MakeGenericMethod(elementType);
                                methodInfo        = GenericType.Get(elementType).BinarySerializeStructArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                        }
                        else
                        {
                            //methodInfo = SerializeMethodCache.ArrayMethod.MakeGenericMethod(elementType);
                            methodInfo        = ClassGenericType.Get(elementType).BinarySerializeArrayMethod;
                            IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                        }
                        DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                        return;
                    }
                }
                DefaultSerializer = toNull;
                IsReferenceMember = false;
                return;
            }
            if (type.IsEnum)
            {
                Type enumType = System.Enum.GetUnderlyingType(type);
                if (enumType == typeof(uint))
                {
                    DefaultSerializer = enumUInt;
                }
                else if (enumType == typeof(byte))
                {
                    DefaultSerializer = enumByte;
                }
                else if (enumType == typeof(ulong))
                {
                    DefaultSerializer = enumULong;
                }
                else if (enumType == typeof(ushort))
                {
                    DefaultSerializer = enumUShort;
                }
                else if (enumType == typeof(long))
                {
                    DefaultSerializer = enumLong;
                }
                else if (enumType == typeof(short))
                {
                    DefaultSerializer = enumShort;
                }
                else if (enumType == typeof(sbyte))
                {
                    DefaultSerializer = enumSByte;
                }
                else
                {
                    DefaultSerializer = enumInt;
                }
                isValueType       = true;
                IsReferenceMember = false;
                return;
            }
            if (type.IsPointer || typeof(Delegate).IsAssignableFrom(type))
            {
                DefaultSerializer = toNull;
                IsReferenceMember = false;
                isValueType       = true;
                return;
            }

            if (type.IsGenericType)
            {
                Type   genericType    = type.GetGenericTypeDefinition();
                Type[] parameterTypes = type.GetGenericArguments();
                if (genericType == typeof(LeftArray <>))
                {
                    //DefaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.LeftArraySerializeMethod.MakeGenericMethod(parameterTypes));
                    DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), GenericType.Get(parameterTypes[0]).BinarySerializeLeftArrayMethod);
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType       = true;
                    return;
                }
#if !Serialize
                if (genericType == typeof(SubArray <>))
                {
                    //DefaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.SubArraySerializeMethod.MakeGenericMethod(parameterTypes));
                    DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), GenericType.Get(parameterTypes[0]).BinarySerializeSubArrayMethod);
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType       = true;
                    return;
                }
#endif
                if (genericType == typeof(Dictionary <,>) || genericType == typeof(SortedDictionary <,>) || genericType == typeof(SortedList <,>))
                {
                    //DefaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.DictionarySerializeMethod.MakeGenericMethod(type, parameterTypes[0], parameterTypes[1]));
                    DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), DictionaryGenericType3.Get(type, parameterTypes[0], parameterTypes[1]).BinarySerializeDictionaryMethod);
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType       = true;
                    return;
                }
                if (genericType == typeof(Nullable <>))
                {
                    //DefaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.NullableSerializeMethod.MakeGenericMethod(parameterTypes));
                    DefaultSerializer = (Action <Serializer, valueType>)StructGenericType.Get(parameterTypes[0]).BinarySerializeNullableDelegate;
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType       = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    //DefaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.KeyValuePairSerializeMethod.MakeGenericMethod(parameterTypes));
                    DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), GenericType2.Get(parameterTypes).BinarySerializeKeyValuePairMethod);
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType       = true;
                    return;
                }
            }
            if ((methodInfo = SerializeMethodCache.GetCustom(type, true)) != null)
            {
                if (type.IsValueType)
                {
#if NOJIT
                    DefaultSerializer = new CustomSerializer(methodInfo).Serialize;
#else
                    DynamicMethod dynamicMethod = new DynamicMethod("CustomSerializer", null, new Type[] { typeof(Serializer), type }, type, true);
                    ILGenerator   generator     = dynamicMethod.GetILGenerator();
                    generator.Emit(OpCodes.Ldarga_S, 1);
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.call(methodInfo);
                    generator.Emit(OpCodes.Ret);
                    DefaultSerializer = (Action <Serializer, valueType>)dynamicMethod.CreateDelegate(typeof(Action <Serializer, valueType>));
#endif
                }
                else
                {
                    DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                }
                IsReferenceMember = attribute.IsReferenceMember;
                isValueType       = true;
                return;
            }
            if (type.IsInterface || type.IsAbstract || Emit.Constructor <valueType> .New == null)
            {
                DefaultSerializer = noConstructor;
                isValueType       = IsReferenceMember = true;
                return;
            }
            ConstructorInfo constructorInfo = null;
            Type            argumentType    = null;
            IsReferenceMember = attribute.IsReferenceMember;
            foreach (Type interfaceType in type.GetInterfaces())
            {
                if (interfaceType.IsGenericType)
                {
                    Type genericType = interfaceType.GetGenericTypeDefinition();
                    if (genericType == typeof(ICollection <>))
                    {
                        Type[] parameterTypes = interfaceType.GetGenericArguments();
                        argumentType      = parameterTypes[0];
                        parameterTypes[0] = argumentType.MakeArrayType();
                        constructorInfo   = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);
                        if (constructorInfo != null)
                        {
                            break;
                        }
                        parameterTypes[0] = typeof(IList <>).MakeGenericType(argumentType);
                        constructorInfo   = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);
                        if (constructorInfo != null)
                        {
                            break;
                        }
                        parameterTypes[0] = typeof(ICollection <>).MakeGenericType(argumentType);
                        constructorInfo   = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);
                        if (constructorInfo != null)
                        {
                            break;
                        }
                        parameterTypes[0] = typeof(IEnumerable <>).MakeGenericType(argumentType);
                        constructorInfo   = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);
                        if (constructorInfo != null)
                        {
                            break;
                        }
                    }
                    else if (genericType == typeof(IDictionary <,>))
                    {
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { interfaceType }, null);
                        if (constructorInfo != null)
                        {
                            Type[] parameters = interfaceType.GetGenericArguments();
                            //methodInfo = (type.IsValueType ? SerializeMethodCache.StructDictionaryMethod : SerializeMethodCache.ClassDictionaryMethod).MakeGenericMethod(type, parameters[0], parameters[1]);
                            if (type.IsValueType)
                            {
                                methodInfo = DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinarySerializeStructDictionaryMethod;
                            }
                            else
                            {
                                methodInfo = DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinarySerializeClassDictionaryMethod;
                            }
                            DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                            return;
                        }
                    }
                }
            }
            if (constructorInfo != null)
            {
                if (argumentType.IsValueType && argumentType.IsEnum)
                {
                    Type enumType = System.Enum.GetUnderlyingType(argumentType);
                    //if (enumType == typeof(uint)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumUIntCollectionMethod : SerializeMethodCache.ClassEnumUIntCollectionMethod;
                    //else if (enumType == typeof(byte)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumByteCollectionMethod : SerializeMethodCache.ClassEnumByteCollectionMethod;
                    //else if (enumType == typeof(ulong)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumULongCollectionMethod : SerializeMethodCache.ClassEnumULongCollectionMethod;
                    //else if (enumType == typeof(ushort)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumUShortCollectionMethod : SerializeMethodCache.ClassEnumUShortCollectionMethod;
                    //else if (enumType == typeof(long)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumLongCollectionMethod : SerializeMethodCache.ClassEnumLongCollectionMethod;
                    //else if (enumType == typeof(short)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumShortCollectionMethod : SerializeMethodCache.ClassEnumShortCollectionMethod;
                    //else if (enumType == typeof(sbyte)) methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumSByteCollectionMethod : SerializeMethodCache.ClassEnumSByteCollectionMethod;
                    //else methodInfo = type.IsValueType ? SerializeMethodCache.StructEnumIntCollectionMethod : SerializeMethodCache.ClassEnumIntCollectionMethod;
                    //methodInfo = methodInfo.MakeGenericMethod(argumentType, type);
                    if (enumType == typeof(uint))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumUIntCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumUIntCollectionMethod;
                    }
                    else if (enumType == typeof(byte))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumByteCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumByteCollectionMethod;
                    }
                    else if (enumType == typeof(ulong))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumULongCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumULongCollectionMethod;
                    }
                    else if (enumType == typeof(ushort))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumUShortCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumUShortCollectionMethod;
                    }
                    else if (enumType == typeof(long))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumLongCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumLongCollectionMethod;
                    }
                    else if (enumType == typeof(short))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumShortCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumShortCollectionMethod;
                    }
                    else if (enumType == typeof(sbyte))
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumSByteCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumSByteCollectionMethod;
                    }
                    else
                    {
                        methodInfo = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumIntCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumIntCollectionMethod;
                    }
                }
                //else methodInfo = (type.IsValueType ? SerializeMethodCache.StructCollectionMethod : SerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(argumentType, type);
                else
                {
                    if (type.IsValueType)
                    {
                        methodInfo = CollectionGenericType2.Get(type, argumentType).BinarySerializeStructCollectionMethod;
                    }
                    else
                    {
                        methodInfo = CollectionGenericType2.Get(type, argumentType).BinarySerializeClassCollectionMethod;
                    }
                }
                DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                return;
            }
            if (type.IsValueType)
            {
                isValueType = true;
            }
            else if (attribute != Serializer.DefaultAttribute && attributeType != type)
            {
                for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                {
                    SerializeAttribute baseAttribute = baseType.customAttribute <SerializeAttribute>();
                    if (baseAttribute != null)
                    {
                        if (baseAttribute.IsBaseType)
                        {
                            methodInfo        = SerializeMethodCache.BaseSerializeMethod.MakeGenericMethod(baseType, type);
                            DefaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                            return;
                        }
                        break;
                    }
                }
            }
            isJson      = attribute.GetIsJson;
            isMemberMap = attribute.GetIsMemberMap;
            Fields <FieldSize> fields = SerializeMethodCache.GetFields(attribute.IsAnonymousFields ? MemberIndexGroup <valueType> .GetAnonymousFields(attribute.MemberFilters) : MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), isJson, out memberCountVerify);
            //if (!type.IsValueType && (fields.FixedFields.length | fields.Fields.length | fields.JsonFields.length) == 0)
            //{
            //    DefaultSerializer = noMember;
            //    isValueType = true;
            //    IsReferenceMember = false;
            //    return;
            //}
            fixedFillSize = -fields.FixedSize & 3;
            fixedSize     = (fields.FixedSize + (sizeof(int) + 3)) & (int.MaxValue - 3);
#if NOJIT
            fixedMemberSerializer = new FieldFerializer(ref fields.FixedFields).Serialize;
            if (isMemberMap)
            {
                fixedMemberMapSerializer = new MemberMapSerializer(ref fields.FixedFields).Serialize;
            }
            if (fields.FieldArray.Length != 0)
            {
                memberSerializer = new FieldFerializer(ref fields.FieldArray).Serialize;
                if (isMemberMap)
                {
                    memberMapSerializer = new MemberMapSerializer(ref fields.FieldArray).Serialize;
                }
            }
#else
            SerializeMemberDynamicMethod    fixedDynamicMethod          = new SerializeMemberDynamicMethod(type);
            SerializeMemberMapDynamicMethod fixedMemberMapDynamicMethod = isMemberMap ? new SerializeMemberMapDynamicMethod(type) : default(SerializeMemberMapDynamicMethod);
            foreach (FieldSize member in fields.FixedFields)
            {
                fixedDynamicMethod.Push(member);
                if (isMemberMap)
                {
                    fixedMemberMapDynamicMethod.Push(member);
                }
            }
            fixedMemberSerializer = (Action <Serializer, valueType>)fixedDynamicMethod.Create <Action <Serializer, valueType> >();
            if (isMemberMap)
            {
                fixedMemberMapSerializer = (Action <MemberMap, Serializer, valueType>)fixedMemberMapDynamicMethod.Create <Action <MemberMap, Serializer, valueType> >();
            }

            if (fields.FieldArray.Length != 0)
            {
                SerializeMemberDynamicMethod    dynamicMethod          = new SerializeMemberDynamicMethod(type);
                SerializeMemberMapDynamicMethod memberMapDynamicMethod = isMemberMap ? new SerializeMemberMapDynamicMethod(type) : default(SerializeMemberMapDynamicMethod);
                foreach (FieldSize member in fields.FieldArray)
                {
                    dynamicMethod.Push(member);
                    if (isMemberMap)
                    {
                        memberMapDynamicMethod.Push(member);
                    }
                }
                memberSerializer = (Action <Serializer, valueType>)dynamicMethod.Create <Action <Serializer, valueType> >();
                if (isMemberMap)
                {
                    memberMapSerializer = (Action <MemberMap, Serializer, valueType>)memberMapDynamicMethod.Create <Action <MemberMap, Serializer, valueType> >();
                }
            }
#endif
            if (fields.JsonFields.Length != 0)
            {
                jsonMemberMap    = new MemberMap <valueType>();
                jsonMemberIndexs = new int[fields.JsonFields.Length];
                int index = 0;
                foreach (FieldIndex field in fields.JsonFields)
                {
                    jsonMemberMap.SetMember(jsonMemberIndexs[index++] = field.MemberIndex);
                }
            }
        }
Example #4
0
        static TypeDeSerializer()
        {
            Type       type = typeof(valueType), attributeType;
            MethodInfo methodInfo = DeSerializer.GetDeSerializeMethod(type);

            attribute = type.customAttribute <SerializeAttribute>(out attributeType) ?? Serializer.DefaultAttribute;
            if (methodInfo != null)
            {
                DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                IsReferenceMember   = false;
                isValueType         = true;
                return;
            }
            if (type.IsArray)
            {
                isValueType = true;
                if (type.GetArrayRank() == 1)
                {
                    Type elementType = type.GetElementType();
                    if (!elementType.IsPointer && !typeof(Delegate).IsAssignableFrom(elementType))
                    {
                        if (elementType.IsValueType)
                        {
                            if (elementType.IsEnum)
                            {
                                Type enumType = System.Enum.GetUnderlyingType(elementType);
                                if (enumType == typeof(uint))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumUIntArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(byte))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumByteArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(ulong))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumULongArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(ushort))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumUShortArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(long))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumLongArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(short))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumShortArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(sbyte))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumSByteArrayMethod.MakeGenericMethod(elementType);
                                }
                                else
                                {
                                    methodInfo = DeSerializeMethodCache.EnumIntArrayMethod.MakeGenericMethod(elementType);
                                }
                                IsReferenceMember = false;
                            }
                            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                methodInfo        = DeSerializeMethodCache.NullableArrayMethod.MakeGenericMethod(elementType = elementType.GetGenericArguments()[0]);
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                            else
                            {
                                methodInfo        = DeSerializeMethodCache.StructArrayMethod.MakeGenericMethod(elementType);
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                        }
                        else
                        {
                            methodInfo        = DeSerializeMethodCache.ArrayMethod.MakeGenericMethod(elementType);
                            IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                        }
                        DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                        return;
                    }
                }
                DefaultDeSerializer = fromNull;
                IsReferenceMember   = false;
                return;
            }
            if (type.IsEnum)
            {
                Type enumType = System.Enum.GetUnderlyingType(type);
                if (enumType == typeof(uint))
                {
                    DefaultDeSerializer = enumUInt;
                }
                else if (enumType == typeof(byte))
                {
                    DefaultDeSerializer = enumByte;
                }
                else if (enumType == typeof(ulong))
                {
                    DefaultDeSerializer = enumULong;
                }
                else if (enumType == typeof(ushort))
                {
                    DefaultDeSerializer = enumUShort;
                }
                else if (enumType == typeof(long))
                {
                    DefaultDeSerializer = enumLong;
                }
                else if (enumType == typeof(short))
                {
                    DefaultDeSerializer = enumShort;
                }
                else if (enumType == typeof(sbyte))
                {
                    DefaultDeSerializer = enumSByte;
                }
                else
                {
                    DefaultDeSerializer = enumInt;
                }
                IsReferenceMember = false;
                isValueType       = true;
                return;
            }
            if (type.IsPointer || typeof(Delegate).IsAssignableFrom(type))
            {
                DefaultDeSerializer = fromNull;
                IsReferenceMember   = false;
                isValueType         = true;
                return;
            }
            if (type.IsGenericType)
            {
                Type   genericType    = type.GetGenericTypeDefinition();
                Type[] parameterTypes = type.GetGenericArguments();
                if (genericType == typeof(LeftArray <>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.LeftArrayDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#if !Serialize
                if (genericType == typeof(SubArray <>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.SubArrayDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#endif
                if (genericType == typeof(Dictionary <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.DictionaryDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(Nullable <>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.NullableDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.KeyValuePairDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(SortedDictionary <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.SortedDictionaryDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(SortedList <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.SortedListDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
            }
            if ((methodInfo = SerializeMethodCache.GetCustom(type, false)) != null)
            {
                if (type.IsValueType)
                {
#if NOJIT
                    DefaultDeSerializer = new CustomDeSerializer(methodInfo).DeSerialize;
#else
                    DynamicMethod dynamicMethod = new DynamicMethod("CustomDeSerializer", null, new Type[] { typeof(DeSerializer), type.MakeByRefType() }, type, true);
                    ILGenerator   generator     = dynamicMethod.GetILGenerator();
                    generator.Emit(OpCodes.Ldarg_1);
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.call(methodInfo);
                    generator.Emit(OpCodes.Ret);
                    DefaultDeSerializer = (deSerialize)dynamicMethod.CreateDelegate(typeof(deSerialize));
#endif
                }
                else
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                }
                IsReferenceMember = attribute.IsReferenceMember;
                isValueType       = true;
                return;
            }
            if (type.IsAbstract || type.IsInterface || Emit.Constructor <valueType> .New == null)
            {
                DefaultDeSerializer = noConstructor;
                isValueType         = IsReferenceMember = true;
                return;
            }
            IsReferenceMember = attribute.IsReferenceMember;
            foreach (Type interfaceType in type.GetInterfaces())
            {
                if (interfaceType.IsGenericType)
                {
                    Type genericType = interfaceType.GetGenericTypeDefinition();
                    if (genericType == typeof(ICollection <>))
                    {
                        Type[] parameters   = interfaceType.GetGenericArguments();
                        Type   argumentType = parameters[0];
                        parameters[0] = argumentType.MakeArrayType();
                        ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                        parameters[0]   = typeof(IList <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                        parameters[0]   = typeof(ICollection <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                        parameters[0]   = typeof(IEnumerable <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                    }
                    else if (genericType == typeof(IDictionary <,>))
                    {
                        ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { interfaceType }, null);
                        if (constructorInfo != null)
                        {
                            Type[] parameters = interfaceType.GetGenericArguments();
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructDictionaryDeSerializeMethod : DeSerializeMethodCache.ClassDictionaryDeSerializeMethod).MakeGenericMethod(type, parameters[0], parameters[1]);
                            break;
                        }
                    }
                }
            }
            if (methodInfo != null)
            {
                DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                return;
            }
            if (type.IsValueType)
            {
                isValueType = true;
            }
            else if (attribute != Serializer.DefaultAttribute && attributeType != type)
            {
                for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                {
                    SerializeAttribute baseAttribute = baseType.customAttribute <SerializeAttribute>();
                    if (baseAttribute != null)
                    {
                        if (baseAttribute.IsBaseType)
                        {
                            methodInfo          = DeSerializeMethodCache.BaseSerializeMethod.MakeGenericMethod(baseType, type);
                            DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                            return;
                        }
                        break;
                    }
                }
            }

            LeftArray <DeSerializeVersionField> attributeFields = new LeftArray <DeSerializeVersionField>();
            if ((attribute.MemberFilters & MemberFilters.PublicInstanceField) != 0)
            {
                appendField(ref attributeFields, MemberIndexGroup <valueType> .Group.PublicFields);
            }
            else
            {
                foreach (FieldIndex field in MemberIndexGroup <valueType> .Group.PublicFields)
                {
                    attributeFields.Add(new DeSerializeVersionField {
                        Field = field
                    });
                }
            }
            if ((attribute.MemberFilters & MemberFilters.NonPublicInstanceField) != 0)
            {
                appendField(ref attributeFields, MemberIndexGroup <valueType> .Group.NonPublicFields);
            }
            if (attribute.IsAnonymousFields)
            {
                appendField(ref attributeFields, MemberIndexGroup <valueType> .Group.AnonymousFields);
            }
            foreach (FieldIndex field in new MemberIndexGroup(type, true).NonPublicFields)
            {
                Type fieldType = field.Member.FieldType;
                if (!fieldType.IsPointer && (!fieldType.IsArray || fieldType.GetArrayRank() == 1) && !field.IsIgnore && !typeof(Delegate).IsAssignableFrom(fieldType))
                {
                    SerializeMemberAttribute memberAttribute = field.GetAttribute <SerializeMemberAttribute>(true);
                    if (memberAttribute != null && memberAttribute.IsSetup && memberAttribute.RemoveGlobalVersion != memberAttribute.GlobalVersion)
                    {
                        attributeFields.Add(new DeSerializeVersionField {
                            Field = field, Attribute = memberAttribute
                        });
                        if (memberAttribute.IsRemove)
                        {
                            attributeFields.Add(new DeSerializeVersionField {
                                Field = field, Attribute = memberAttribute, IsRemove = true
                            });
                        }
                    }
                }
            }

            isMemberMap = attribute.GetIsMemberMap;
            isJson      = attribute.GetIsJson;
            uint globalVersion = 0, removeMemberCount = 0;
            int  noSerializeMemberCount = 0;
            LeftArray <DeSerializeVersionFields <valueType> > deSerializeVersionFields = default(LeftArray <DeSerializeVersionFields <valueType> >);
            LeftArray <DeSerializeVersionField> attributeVersionFields = new LeftArray <DeSerializeVersionField>(attributeFields.Length);
            foreach (DeSerializeVersionField field in attributeFields.Sort(DeSerializeVersionField.GlobalVersionSort))
            {
                if (field.GlobalVersion != globalVersion)
                {
                    deSerializeVersionFields.Add(new DeSerializeVersionFields <valueType>(globalVersion, attributeVersionFields.GetArray(), removeMemberCount, noSerializeMemberCount));
                    globalVersion = field.GlobalVersion;
                }
                if (field.IsRemove)
                {
                    attributeVersionFields.RemoveToEnd(value => object.ReferenceEquals(value.Field, field.Field));
                    if (field.Attribute.IsRemove)
                    {
                        --removeMemberCount;
                    }
                }
                else
                {
                    attributeVersionFields.Add(field);
                    if (field.Attribute != null)
                    {
                        if (field.Attribute.IsRemove)
                        {
                            ++removeMemberCount;
                        }
                    }
                    else
                    {
                        ++noSerializeMemberCount;
                    }
                }
            }
            fieldDeSerializer = new DeSerializeVersionFields <valueType>(globalVersion, attributeVersionFields.GetArray(), removeMemberCount, noSerializeMemberCount).CreateOnly(attribute);
            if (deSerializeVersionFields.Length != 0)
            {
                int count = deSerializeVersionFields.Length;
                DeSerializeVersionFields <valueType>[] deSerializeVersionFieldsArray = new DeSerializeVersionFields <valueType> [count];
                foreach (DeSerializeVersionFields <valueType> value in deSerializeVersionFields)
                {
                    deSerializeVersionFieldsArray[--count]          = value;
                    deSerializeVersionFieldsArray[count].CreateLock = new object();
                }
                fieldDeSerializers = deSerializeVersionFieldsArray;
            }
        }