Пример #1
0
        /// <summary>
        /// 获取成员转换函数信息
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="isCustom"></param>
        /// <returns>成员转换函数信息</returns>
        internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom)
        {
            MethodInfo methodInfo = JsonDeSerializer.GetDeSerializeMethod(type);

            if (methodInfo != null)
            {
                return(methodInfo);
            }
            if (type.IsArray)
            {
                return(GenericType.Get(type.GetElementType()).JsonDeSerializeArrayMethod.Method);
            }
            if (type.IsEnum)
            {
                if (type.IsDefined(typeof(FlagsAttribute), false))
                {
                    return((EnumGenericType.Get(type).JsonDeSerializeEnumFlagsDelegate).Method);
                }
                return((EnumGenericType.Get(type).JsonDeSerializeEnumDelegate).Method);
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Dictionary <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).JsonDeSerializeDictionaryMethod.Method);
                }
                if (genericType == typeof(Nullable <>))
                {
                    return(GetNullable(type).Method);
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).JsonDeSerializeKeyValuePairMethod.Method);
                }
            }
            if ((methodInfo = GetCustom(type)) != null)
            {
                isCustom = type.IsValueType;
                return(methodInfo);
            }
            if ((methodInfo = GetIEnumerableConstructor(type)) != null)
            {
                return(methodInfo);
            }
            if (type.IsValueType)
            {
                return(StructGenericType.Get(type).JsonDeSerializeStructMethod);
            }
            return(GenericType.Get(type).JsonDeSerializeTypeMethod);
        }
Пример #2
0
        static TypeDeSerializer()
        {
            Type       type       = typeof(T);
            MethodInfo methodInfo = XmlDeSerializer.GetDeSerializeMethod(type);

            if (methodInfo != null)
            {
                DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo);
                return;
            }
            if (type.IsArray)
            {
                if (type.GetArrayRank() == 1)
                {
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type.GetElementType()).XmlDeSerializeArrayMethod;
                }
                else
                {
                    DefaultDeSerializer = notSupport;
                }
                return;
            }
            if (type.IsEnum)
            {
                if (type.IsDefined(typeof(FlagsAttribute), false))
                {
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).XmlDeSerializeEnumFlagsDelegate;
                }
                else
                {
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).XmlDeSerializeEnumDelegate;
                }
                return;
            }
            if (type.IsInterface || type.IsPointer || typeof(Delegate).IsAssignableFrom(type))
            {
                DefaultDeSerializer = notSupport;
                return;
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Nullable <>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)(parameterTypes[0].IsEnum ? StructGenericType.Get(parameterTypes[0]).XmlDeSerializeNullableEnumMethod : StructGenericType.Get(parameterTypes[0]).XmlDeSerializeNullableMethod);
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).XmlDeSerializeKeyValuePairMethod;
                    isValueType         = true;
                    return;
                }
            }
            if ((methodInfo = DeSerializeMethodCache.GetCustom(type)) != null)
            {
                if (type.IsValueType)
                {
#if NOJIT
                    DefaultDeSerializer = new CustomDeSerializer(methodInfo).DeSerialize;
#else
                    DynamicMethod dynamicMethod = new DynamicMethod("CustomXmlDeSerializer", null, new Type[] { typeof(XmlDeSerializer), 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 = (XmlDeSerializer.DeSerializeDelegate <T>)dynamicMethod.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>));
#endif
                }
                else
                {
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo);
                }
            }
            else
            {
                Type attributeType;
                attribute = type.customAttribute <XmlSerializeAttribute>(out attributeType) ?? XmlSerializer.AllMemberAttribute;
                if ((methodInfo = DeSerializeMethodCache.GetIEnumerableConstructor(type)) != null)
                {
                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo);
                }
                else
                {
                    if (type.IsValueType)
                    {
                        isValueType = true;
                    }
                    else if (attribute != XmlSerializer.AllMemberAttribute && attributeType != type)
                    {
                        for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                        {
                            XmlSerializeAttribute baseAttribute = baseType.customAttribute <XmlSerializeAttribute>();
                            if (baseAttribute != null)
                            {
                                if (baseAttribute.IsBaseType)
                                {
                                    methodInfo          = DeSerializeMethodCache.BaseDeSerializeMethod.MakeGenericMethod(baseType, type);
                                    DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo);
                                    return;
                                }
                                break;
                            }
                        }
                    }
                    if (type.IsValueType)
                    {
                        foreach (AutoCSer.Metadata.AttributeMethod attributeMethod in AutoCSer.Metadata.AttributeMethod.GetStatic(type))
                        {
                            if (attributeMethod.Method.ReturnType == typeof(bool))
                            {
                                ParameterInfo[] parameters = attributeMethod.Method.GetParameters();
                                if (parameters.Length == 2 && parameters[0].ParameterType == typeof(XmlDeSerializer) && parameters[1].ParameterType == Emit.Pub.PointerSizeRefType)
                                {
                                    if (attributeMethod.GetAttribute <UnknownNameAttribute>() != null)
                                    {
#if NOJIT
                                        onUnknownName = new UnknownDeSerializer(methodInfo).DeSerialize;
#else
                                        DynamicMethod dynamicMethod = new DynamicMethod("XmlUnknownDeSerialize", null, new Type[] { typeof(XmlDeSerializer), type.MakeByRefType(), Emit.Pub.PointerSizeRefType }, type, true);
                                        ILGenerator   generator     = dynamicMethod.GetILGenerator();
                                        generator.Emit(OpCodes.Ldarg_1);
                                        generator.Emit(OpCodes.Ldarg_0);
                                        generator.Emit(OpCodes.Ldarg_2);
                                        generator.call(methodInfo);
                                        generator.Emit(OpCodes.Ret);
                                        onUnknownName = (UnknownDeSerialize)dynamicMethod.CreateDelegate(typeof(UnknownDeSerialize));
#endif
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Type refType = type.MakeByRefType();
                        foreach (AutoCSer.Metadata.AttributeMethod attributeMethod in AutoCSer.Metadata.AttributeMethod.GetStatic(type))
                        {
                            if (attributeMethod.Method.ReturnType == typeof(bool))
                            {
                                ParameterInfo[] parameters = attributeMethod.Method.GetParameters();
                                if (parameters.Length == 3 && parameters[0].ParameterType == typeof(XmlDeSerializer) && parameters[1].ParameterType == refType && parameters[2].ParameterType == Emit.Pub.PointerSizeRefType)
                                {
                                    if (attributeMethod.GetAttribute <UnknownNameAttribute>() != null)
                                    {
                                        onUnknownName = (UnknownDeSerialize)Delegate.CreateDelegate(typeof(UnknownDeSerialize), attributeMethod.Method);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    FieldIndex defaultMember = null;
                    LeftArray <KeyValue <FieldIndex, XmlSerializeMemberAttribute> > fields = SerializeMethodCache.GetFields(MemberIndexGroup <T> .GetFields(attribute.MemberFilters), attribute);
                    LeftArray <PropertyMethod> properties = DeSerializeMethodCache.GetProperties(MemberIndexGroup <T> .GetProperties(attribute.MemberFilters), attribute);
                    bool isBox = false;
                    if (type.IsValueType && fields.Length + properties.Length == 1)
                    {
                        BoxSerializeAttribute boxSerialize = AutoCSer.Metadata.TypeAttribute.GetAttribute <BoxSerializeAttribute>(type);
                        if (boxSerialize != null && boxSerialize.IsXml)
                        {
                            isBox         = true;
                            defaultMember = null;
                        }
                    }
                    TryDeSerializeFilter[] deSerializers = new TryDeSerializeFilter[fields.Length + properties.Length + (defaultMember == null ? 0 : 1)];
                    //memberMap.type memberMapType = memberMap<valueType>.TypeInfo;
                    string[] names = isBox ? null : new string[deSerializers.Length];
                    int      index = 0, nameLength = 0, maxNameLength = 0;
                    foreach (KeyValue <FieldIndex, XmlSerializeMemberAttribute> member in fields)
                    {
                        TryDeSerializeFilter tryDeSerialize = deSerializers[index] = new TryDeSerializeFilter
                        {
#if NOJIT
                            TryDeSerialize = new FieldDeSerializer(member.Key.Member).DeSerializer(),
#else
                            TryDeSerialize = (XmlDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.CreateDynamicMethod(type, member.Key.Member).CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>)),
#endif
                            ItemName       = member.Value == null ? null : member.Value.ItemName,
                            MemberMapIndex = member.Key.MemberIndex,
                            //MemberFilter = member.Key.Member.IsPublic ? MemberFilters.PublicInstanceField : MemberFilters.NonPublicInstanceField
                        };
                        if (!isBox)
                        {
                            string name = member.Key.AnonymousName;
                            if (name.Length > maxNameLength)
                            {
                                maxNameLength = name.Length;
                            }
                            nameLength += (names[index++] = name).Length;
                            if (member.Key == defaultMember)
                            {
                                deSerializers[deSerializers.Length - 1] = tryDeSerialize;
                                names[deSerializers.Length - 1]         = string.Empty;
                            }
                        }
                    }
                    foreach (PropertyMethod member in properties)
                    {
                        deSerializers[index] = new TryDeSerializeFilter
                        {
#if NOJIT
                            TryDeSerialize = new PropertyDeSerializer(member.Property.Member).DeSerializer(),
#else
                            TryDeSerialize = (XmlDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.CreateDynamicMethod(type, member.Property.Member, member.Method).CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>)),
#endif
                            ItemName       = member.Attribute == null ? null : member.Attribute.ItemName,
                            MemberMapIndex = member.Property.MemberIndex,
                            //MemberFilter = member.Method.IsPublic ? MemberFilters.PublicInstanceProperty : MemberFilters.NonPublicInstanceProperty
                        };
                        if (!isBox)
                        {
                            if (member.Property.Member.Name.Length > maxNameLength)
                            {
                                maxNameLength = member.Property.Member.Name.Length;
                            }
                            nameLength += (names[index++] = member.Property.Member.Name).Length;
                        }
                    }
                    memberDeSerializers = deSerializers;
                    if (isBox)
                    {
                        DefaultDeSerializer = unbox;
                    }
                    else
                    {
                        if (type.Name[0] == '<')
                        {
                            isAnonymousType = true;
                        }
                        if (maxNameLength > (short.MaxValue >> 1) - 2 || nameLength == 0)
                        {
                            memberNames = Unmanaged.NullByte8;
                        }
                        else
                        {
                            memberNames = Unmanaged.GetStaticPointer((nameLength + (names.Length - (defaultMember == null ? 0 : 1)) * 3 + 1) << 1, false);
                            byte *write = memberNames.Byte;
                            foreach (string name in names)
                            {
                                if (name.Length != 0)
                                {
                                    *(short *)write = (short)((name.Length + 2) * sizeof(char));
                                    *(char *)(write + sizeof(short)) = '<';

                                    fixed(char *nameFixed = name) AutoCSer.Extensions.StringExtension.SimpleCopyNotNull(nameFixed, (char *)(write + (sizeof(short) + sizeof(char))), name.Length);

                                    *(char *)(write += (sizeof(short) + sizeof(char)) + (name.Length << 1)) = '>';
                                    write += sizeof(char);
                                }
                            }
                            *(short *)write = 0;
                        }
                        if (type.IsGenericType)
                        {
                            memberSearcher = DeSerializeMethodCache.GetGenericDefinitionMemberSearcher(type, names);
                        }
                        else
                        {
                            memberSearcher = AutoCSer.StateSearcher.CharBuilder.Create(names, true);
                        }
                    }
                }
            }
        }
Пример #3
0
        static TypeSerializer()
        {
            Type       type = typeof(T), attributeType;
            MethodInfo methodInfo = BinarySerializer.GetSerializeMethod(type);

            attribute = type.customAttribute <BinarySerializeAttribute>(out attributeType) ?? BinarySerializer.DefaultAttribute;
            if (methodInfo != null)
            {
                DefaultSerializer = (Action <BinarySerializer, T>)Delegate.CreateDelegate(typeof(Action <BinarySerializer, T>), 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))
                    {
                        Delegate arrayDelegate;
                        if (elementType.IsValueType)
                        {
                            if (elementType.IsEnum)
                            {
                                arrayDelegate     = EnumGenericType.Get(elementType).BinarySerializeEnumArrayDelegate;
                                IsReferenceMember = false;
                            }
                            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                arrayDelegate     = StructGenericType.Get(elementType = elementType.GetGenericArguments()[0]).BinarySerializeNullableArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                            else
                            {
                                arrayDelegate     = GenericType.Get(elementType).BinarySerializeStructArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                        }
                        else
                        {
                            arrayDelegate     = ClassGenericType.Get(elementType).BinarySerializeArrayMethod;
                            IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                        }
                        DefaultSerializer = (Action <BinarySerializer, T>)arrayDelegate;
                        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 <BinarySerializer, T>)GenericType.Get(parameterTypes[0]).BinarySerializeLeftArrayMethod;
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType       = true;
                    return;
                }
#if !Serialize
                if (genericType == typeof(SubArray <>))
                {
                    DefaultSerializer = (Action <BinarySerializer, T>)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 <BinarySerializer, T>)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 <BinarySerializer, T>)StructGenericType.Get(parameterTypes[0]).BinarySerializeNullableDelegate;
                    IsReferenceMember = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType       = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    DefaultSerializer = (Action <BinarySerializer, T>)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(BinarySerializer), 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 <BinarySerializer, T>)dynamicMethod.CreateDelegate(typeof(Action <BinarySerializer, T>));
#endif
                }
                else
                {
                    DefaultSerializer = (Action <BinarySerializer, T>)Delegate.CreateDelegate(typeof(Action <BinarySerializer, T>), methodInfo);
                }
                IsReferenceMember = attribute.IsReferenceMember;
                isValueType       = true;
                return;
            }
            if (type.isSerializeNotSupport())
            {
                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();
                            if (type.IsValueType)
                            {
                                DefaultSerializer = (Action <BinarySerializer, T>)DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinarySerializeStructDictionaryMethod;
                            }
                            else
                            {
                                DefaultSerializer = (Action <BinarySerializer, T>)DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinarySerializeClassDictionaryMethod;
                            }
                            return;
                        }
                    }
                }
            }
            if (constructorInfo != null)
            {
                Delegate collectionDelegate;
                if (argumentType.IsValueType && argumentType.IsEnum)
                {
                    Type enumType = System.Enum.GetUnderlyingType(argumentType);
                    if (enumType == typeof(uint))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumUIntCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumUIntCollectionMethod;
                    }
                    else if (enumType == typeof(byte))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumByteCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumByteCollectionMethod;
                    }
                    else if (enumType == typeof(ulong))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumULongCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumULongCollectionMethod;
                    }
                    else if (enumType == typeof(ushort))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumUShortCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumUShortCollectionMethod;
                    }
                    else if (enumType == typeof(long))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumLongCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumLongCollectionMethod;
                    }
                    else if (enumType == typeof(short))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumShortCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumShortCollectionMethod;
                    }
                    else if (enumType == typeof(sbyte))
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumSByteCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumSByteCollectionMethod;
                    }
                    else
                    {
                        collectionDelegate = type.IsValueType ? CollectionGenericType2.Get(type, argumentType).BinarySerializeStructEnumIntCollectionMethod : CollectionGenericType2.Get(type, argumentType).BinarySerializeClassEnumIntCollectionMethod;
                    }
                }
                else
                {
                    if (type.IsValueType)
                    {
                        collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinarySerializeStructCollectionMethod;
                    }
                    else
                    {
                        collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinarySerializeClassCollectionMethod;
                    }
                }
                DefaultSerializer = (Action <BinarySerializer, T>)collectionDelegate;
                return;
            }
            if (type.IsValueType)
            {
                isValueType = true;
            }
            else if (attribute != BinarySerializer.DefaultAttribute && attributeType != type)
            {
                for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                {
                    BinarySerializeAttribute baseAttribute = baseType.customAttribute <BinarySerializeAttribute>();
                    if (baseAttribute != null)
                    {
                        if (baseAttribute.IsBaseType)
                        {
                            methodInfo        = SerializeMethodCache.BaseSerializeMethod.MakeGenericMethod(baseType, type);
                            DefaultSerializer = (Action <BinarySerializer, T>)Delegate.CreateDelegate(typeof(Action <BinarySerializer, T>), methodInfo);
                            return;
                        }
                        break;
                    }
                }
            }
            isJson      = attribute.GetIsJson;
            isMemberMap = attribute.GetIsMemberMap;
            Fields <FieldSize> fields = SerializeMethodCache.GetFields(attribute.IsAnonymousFields ? MemberIndexGroup <T> .GetAnonymousFields(attribute.MemberFilters) : MemberIndexGroup <T> .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 <BinarySerializer, T>)fixedDynamicMethod.Create <Action <BinarySerializer, T> >();
            if (isMemberMap)
            {
                fixedMemberMapSerializer = (Action <MemberMap, BinarySerializer, T>)fixedMemberMapDynamicMethod.Create <Action <MemberMap, BinarySerializer, T> >();
            }

            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 <BinarySerializer, T>)dynamicMethod.Create <Action <BinarySerializer, T> >();
                if (isMemberMap)
                {
                    memberMapSerializer = (Action <MemberMap, BinarySerializer, T>)memberMapDynamicMethod.Create <Action <MemberMap, BinarySerializer, T> >();
                }
            }
#endif
            if (fields.JsonFields.Length != 0)
            {
                jsonMemberMap    = new MemberMap <T>();
                jsonMemberIndexs = new int[fields.JsonFields.Length];
                int index = 0;
                foreach (FieldIndex field in fields.JsonFields)
                {
                    jsonMemberMap.SetMember(jsonMemberIndexs[index++] = field.MemberIndex);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 未知类型枚举反序列化委托调用函数信息
        /// </summary>
        /// <param name="type">数组类型</param>
        /// <returns>未知类型反序列化委托调用函数信息</returns>
        public static MethodInfo GetMember(Type type)
        {
            MethodInfo method;

            if (memberMethods.TryGetValue(type, out method))
            {
                return(method);
            }
            if (type.IsArray)
            {
                Type elementType = type.GetElementType();
                if (elementType.IsValueType)
                {
                    if (elementType.IsEnum)
                    {
                        method = EnumGenericType.Get(elementType).BinaryDeSerializeEnumArrayMemberDelegate.Method;
                    }
                    else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        method = StructGenericType.Get(elementType.GetGenericArguments()[0]).BinaryDeSerializeNullableArrayMemberMethod;
                    }
                    else
                    {
                        method = GenericType.Get(elementType).BinaryDeSerializeStructArrayMemberMethod;
                    }
                }
                else
                {
                    method = ClassGenericType.Get(elementType).BinaryDeSerializeArrayMemberMethod;
                }
            }
            else if (type.IsEnum)
            {
                method = EnumGenericType.Get(type).BinaryDeSerializeEnumMemberDelegate.Method;
            }
            else
            {
                if (type.IsGenericType)
                {
                    Type genericType = type.GetGenericTypeDefinition();
                    if (genericType == typeof(Dictionary <,>))
                    {
                        method = GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeDictionaryMemberMethod;
                    }
                    else if (genericType == typeof(Nullable <>))
                    {
                        method = StructGenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeNullableMemberMethod;
                    }
                    else if (genericType == typeof(KeyValuePair <,>))
                    {
                        method = GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeKeyValuePairMethod.Method;
                    }
                    else if (genericType == typeof(SortedDictionary <,>))
                    {
                        method = GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeSortedDictionaryMemberMethod;
                    }
                    else if (genericType == typeof(SortedList <,>))
                    {
                        method = GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeSortedListMemberMethod;
                    }
                }
                if (method == null)
                {
                    if (type.IsValueType)
                    {
                        method = StructGenericType.Get(type).BinaryDeSerializeStructMethod;                  // structDeSerializeMethod.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = ClassGenericType.Get(type).BinaryDeSerializeMemberClassMethod; // memberClassDeSerializeMethod.MakeGenericMethod(type);
                    }
                }
            }
            memberMethods.Set(type, method);
            return(method);
        }
Пример #5
0
        static TypeDeSerializer()
        {
            Type       type       = typeof(T);
            MethodInfo methodInfo = JsonDeSerializer.GetDeSerializeMethod(type);

            if (methodInfo != null)
            {
                DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>), methodInfo);
                return;
            }
            if (type.IsArray)
            {
                if (type.GetArrayRank() == 1)
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type.GetElementType()).JsonDeSerializeArrayMethod;
                }
                else
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type).JsonDeSerializeNotSupportDelegate;
                }
                return;
            }
            if (type.IsEnum)
            {
                if (type.IsDefined(typeof(FlagsAttribute), false))
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).JsonDeSerializeEnumFlagsDelegate;
                }
                else
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).JsonDeSerializeEnumDelegate;
                }
                return;
            }
            if (type.isSerializeNotSupport())
            {
                DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type).JsonDeSerializeNotSupportDelegate;
                return;
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Dictionary <,>))
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).JsonDeSerializeDictionaryMethod;
                    return;
                }
                if (genericType == typeof(Nullable <>))
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.GetNullable(type);
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).JsonDeSerializeKeyValuePairMethod;
                    isValueType         = true;
                    return;
                }
            }
            if ((methodInfo = DeSerializeMethodCache.GetCustom(type)) != null)
            {
                if (type.IsValueType)
                {
#if NOJIT
                    DefaultDeSerializer = new CustomDeSerializer(methodInfo).DeSerialize;
#else
                    DynamicMethod dynamicMethod = new DynamicMethod("CustomJsonDeSerializer", null, new Type[] { typeof(JsonDeSerializer), 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 = (JsonDeSerializer.DeSerializeDelegate <T>)dynamicMethod.CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>));
#endif
                }
                else
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>), methodInfo);
                }
            }
            else
            {
                Type attributeType;
                attribute = type.customAttribute <JsonDeSerializeAttribute>(out attributeType) ?? JsonDeSerializer.AllMemberAttribute;
                if ((methodInfo = DeSerializeMethodCache.GetIEnumerableConstructor(type)) != null)
                {
                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>), methodInfo);
                }
                else
                {
                    if (type.IsValueType)
                    {
                        isValueType = true;
                    }
                    else if (attribute != JsonDeSerializer.AllMemberAttribute && attributeType != type)
                    {
                        for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                        {
                            JsonDeSerializeAttribute baseAttribute = baseType.customAttribute <JsonDeSerializeAttribute>();
                            if (baseAttribute != null)
                            {
                                if (baseAttribute.IsBaseType)
                                {
                                    methodInfo          = DeSerializeMethodCache.BaseDeSerializeMethod.MakeGenericMethod(baseType, type);
                                    DefaultDeSerializer = (JsonDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>), methodInfo);
                                    return;
                                }
                                break;
                            }
                        }
                    }
                    FieldIndex             defaultMember = null;
                    LeftArray <FieldIndex> fields        = DeSerializeMethodCache.GetFields(MemberIndexGroup <T> .GetFields(attribute.MemberFilters), attribute, ref defaultMember);
                    LeftArray <KeyValue <PropertyIndex, MethodInfo> > properties = DeSerializeMethodCache.GetProperties(MemberIndexGroup <T> .GetProperties(attribute.MemberFilters), attribute);
                    bool isBox = false;
                    if (type.IsValueType && fields.Length + properties.Length == 1)
                    {
                        BoxSerializeAttribute boxSerialize = AutoCSer.Metadata.TypeAttribute.GetAttribute <BoxSerializeAttribute>(type);
                        if (boxSerialize != null && boxSerialize.IsJson)
                        {
                            isBox         = true;
                            defaultMember = null;
                        }
                    }
                    TryDeSerializeFilter[] deSerializers = new TryDeSerializeFilter[fields.Length + properties.Length + (defaultMember == null ? 0 : 1)];
                    //memberMap.type memberMapType = memberMap<valueType>.TypeInfo;
                    string[] names = isBox ? null : new string[deSerializers.Length];
#if !NOJIT
                    DeSerializeDynamicMethod dynamicMethod = isBox ? default(DeSerializeDynamicMethod) : new DeSerializeDynamicMethod(type, false), memberMapDynamicMethod = isBox ? default(DeSerializeDynamicMethod) : new DeSerializeDynamicMethod(type, true);
#endif
                    int index = 0, nameLength = 0, maxNameLength = 0;
                    foreach (FieldIndex member in fields)
                    {
                        TryDeSerializeFilter tryDeSerialize = deSerializers[index] = new TryDeSerializeFilter
                        {
#if NOJIT
                            TryDeSerialize = new FieldDeSerializer(member.Member).DeSerializer(),
#else
                            TryDeSerialize = (JsonDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.CreateDynamicMethod(type, member.Member).CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>)),
#endif
                            MemberMapIndex = member.MemberIndex,
                            //MemberFilters = member.Member.IsPublic ? Metadata.MemberFilters.PublicInstanceField : Metadata.MemberFilters.NonPublicInstanceField
                        };
                        if (!isBox)
                        {
#if !NOJIT
                            dynamicMethod.Push(member);
                            memberMapDynamicMethod.Push(member);
#endif
                            string name = member.AnonymousName;
                            if (name.Length > maxNameLength)
                            {
                                maxNameLength = name.Length;
                            }
                            nameLength += (names[index++] = name).Length;
                            if (member == defaultMember)
                            {
                                deSerializers[deSerializers.Length - 1] = tryDeSerialize;
                                names[deSerializers.Length - 1]         = string.Empty;
                            }
                        }
                    }
                    foreach (KeyValue <PropertyIndex, MethodInfo> member in properties)
                    {
                        deSerializers[index] = new TryDeSerializeFilter
                        {
#if NOJIT
                            TryDeSerialize = new PropertyDeSerializer(member.Key.Member).DeSerializer(),
#else
                            TryDeSerialize = (JsonDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.CreateDynamicMethod(type, member.Key.Member, member.Value).CreateDelegate(typeof(JsonDeSerializer.DeSerializeDelegate <T>)),
#endif
                            MemberMapIndex = member.Key.MemberIndex,
                            //MemberFilters = member.Value.IsPublic ? Metadata.MemberFilters.PublicInstanceProperty : Metadata.MemberFilters.NonPublicInstanceProperty
                        };
                        if (!isBox)
                        {
#if !NOJIT
                            dynamicMethod.Push(member.Key, member.Value);
                            memberMapDynamicMethod.Push(member.Key, member.Value);
#endif
                            if (member.Key.Member.Name.Length > maxNameLength)
                            {
                                maxNameLength = member.Key.Member.Name.Length;
                            }
                            nameLength += (names[index++] = member.Key.Member.Name).Length;
                        }
                    }
                    if (isBox)
                    {
                        unboxDeSerializer   = deSerializers[0].TryDeSerialize;
                        DefaultDeSerializer = unbox;
                    }
                    else
                    {
#if !NOJIT
                        deSerializeMember    = (DeSerializeMember)dynamicMethod.Create <DeSerializeMember>();
                        deSerializeMemberMap = (DeSerializeMemberMap)memberMapDynamicMethod.Create <DeSerializeMemberMap>();
#endif
                        if (type.Name[0] == '<')
                        {
                            isAnonymousType = true;
                        }
                        if (maxNameLength > (short.MaxValue >> 1) - 4 || nameLength == 0)
                        {
                            memberNames = Unmanaged.NullByte8;
                        }
                        else
                        {
                            memberNames = Unmanaged.GetStaticPointer((nameLength + (names.Length - (defaultMember == null ? 0 : 1)) * 5 + 1) << 1, false);
                            byte *write = memberNames.Byte;
                            foreach (string name in names)
                            {
                                if (name.Length != 0)
                                {
                                    if (write == memberNames.Byte)
                                    {
                                        *(short *)write = (short)((name.Length + 3) * sizeof(char));
                                        *(char *)(write + sizeof(short)) = '"';
                                        write += sizeof(short) + sizeof(char);
                                    }
                                    else
                                    {
                                        *(short *)write = (short)((name.Length + 4) * sizeof(char));
                                        *(int *)(write + sizeof(short)) = ',' + ('"' << 16);
                                        write += sizeof(short) + sizeof(int);
                                    }
                                    fixed(char *nameFixed = name) AutoCSer.Extensions.StringExtension.SimpleCopyNotNull(nameFixed, (char *)write, name.Length);

                                    *(int *)(write += name.Length << 1) = '"' + (':' << 16);
                                    write += sizeof(int);
                                }
                            }
                            *(short *)write = 0;
                        }
                        memberSearcher      = new StateSearcher(StateSearcher.GetMemberSearcher(type, names));
                        memberDeSerializers = deSerializers;

                        Type refType = type.MakeByRefType();
                        foreach (AutoCSer.Metadata.AttributeMethod attributeMethod in AutoCSer.Metadata.AttributeMethod.GetStatic(type))
                        {
                            if ((methodInfo = attributeMethod.Method).ReturnType == typeof(void))
                            {
                                ParameterInfo[] parameters = methodInfo.GetParameters();
                                if (parameters.Length == 3 && parameters[0].ParameterType == typeof(JsonDeSerializer) && parameters[1].ParameterType == refType && parameters[2].ParameterType == Emit.Pub.PointerSizeRefType)
                                {
                                    if (attributeMethod.GetAttribute <JsonDeSerializeUnknownNameAttriubte>() != null)
                                    {
                                        onUnknownName = (UnknownDeSerialize)Delegate.CreateDelegate(typeof(UnknownDeSerialize), methodInfo);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #6
0
        static TypeDeSerializer()
        {
            Type       type = typeof(T), attributeType;
            MethodInfo methodInfo = BinaryDeSerializer.GetDeSerializeMethod(type);

            attribute = type.customAttribute <BinarySerializeAttribute>(out attributeType) ?? BinarySerializer.DefaultAttribute;
            if (methodInfo != null)
            {
                DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(BinaryDeSerializer.DeSerializeDelegate <T>), 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))
                    {
                        Delegate arrayDelegate;
                        if (elementType.IsValueType)
                        {
                            if (elementType.IsEnum)
                            {
                                arrayDelegate     = EnumGenericType.Get(elementType).BinaryDeSerializeEnumArrayDelegate;
                                IsReferenceMember = false;
                            }
                            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                arrayDelegate     = StructGenericType.Get(elementType = elementType.GetGenericArguments()[0]).BinaryDeSerializeNullableArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                            else
                            {
                                arrayDelegate     = GenericType.Get(elementType).BinaryDeSerializeStructArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                        }
                        else
                        {
                            arrayDelegate     = ClassGenericType.Get(elementType).BinaryDeSerializeArrayMethod;
                            IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                        }
                        DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)arrayDelegate;
                        return;
                    }
                }
                DefaultDeSerializer = fromNull;
                IsReferenceMember   = false;
                return;
            }
            if (type.IsEnum)
            {
                DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).BinaryDeSerializeEnumDelegate;
                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 = (BinaryDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeLeftArrayMethod;
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#if !Serialize
                if (genericType == typeof(SubArray <>))
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeSubArrayMethod;
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#endif
                if (genericType == typeof(Dictionary <,>))
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeDictionaryMethod;
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(Nullable <>))
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)StructGenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeNullableMethod;
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeKeyValuePairMethod;
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(SortedDictionary <,>))
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeSortedDictionaryMethod;
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(SortedList <,>))
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeSortedListMethod;
                    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(BinaryDeSerializer), 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 = (BinaryDeSerializer.DeSerializeDelegate <T>)dynamicMethod.CreateDelegate(typeof(BinaryDeSerializer.DeSerializeDelegate <T>));
#endif
                }
                else
                {
                    DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(BinaryDeSerializer.DeSerializeDelegate <T>), methodInfo);
                }
                IsReferenceMember = attribute.IsReferenceMember;
                isValueType       = true;
                return;
            }
            if (type.isSerializeNotSupport())
            {
                DefaultDeSerializer = noConstructor;
                isValueType         = IsReferenceMember = true;
                return;
            }
            IsReferenceMember = attribute.IsReferenceMember;
            Delegate collectionDelegate = null;
            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)
                        {
                            if (type.IsValueType)
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeClassCollectionMethod;
                            }
                            break;
                        }
                        parameters[0]   = typeof(IList <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            if (type.IsValueType)
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeClassCollectionMethod;
                            }
                            break;
                        }
                        parameters[0]   = typeof(ICollection <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            if (type.IsValueType)
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeClassCollectionMethod;
                            }
                            break;
                        }
                        parameters[0]   = typeof(IEnumerable <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            if (type.IsValueType)
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                collectionDelegate = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeClassCollectionMethod;
                            }
                            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();
                            if (type.IsValueType)
                            {
                                collectionDelegate = DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinaryDeSerializeStructDictionaryMethod;
                            }
                            else
                            {
                                collectionDelegate = DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinaryDeSerializeClassDictionaryMethod;
                            }
                            break;
                        }
                    }
                }
            }
            if (collectionDelegate != null)
            {
                DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)collectionDelegate;
                return;
            }
            if (type.IsValueType)
            {
                isValueType = true;
            }
            else if (attribute != BinarySerializer.DefaultAttribute && attributeType != type)
            {
                for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                {
                    BinarySerializeAttribute baseAttribute = baseType.customAttribute <BinarySerializeAttribute>();
                    if (baseAttribute != null)
                    {
                        if (baseAttribute.IsBaseType)
                        {
                            methodInfo          = DeSerializeMethodCache.BaseSerializeMethod.MakeGenericMethod(baseType, type);
                            DefaultDeSerializer = (BinaryDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(BinaryDeSerializer.DeSerializeDelegate <T>), methodInfo);
                            return;
                        }
                        break;
                    }
                }
            }

            LeftArray <DeSerializeVersionField> attributeFields = new LeftArray <DeSerializeVersionField>(0);
            if ((attribute.MemberFilters & MemberFilters.PublicInstanceField) != 0)
            {
                appendField(ref attributeFields, MemberIndexGroup <T> .Group.PublicFields);
            }
            else
            {
                foreach (FieldIndex field in MemberIndexGroup <T> .Group.PublicFields)
                {
                    attributeFields.Add(new DeSerializeVersionField {
                        Field = field
                    });
                }
            }
            if ((attribute.MemberFilters & MemberFilters.NonPublicInstanceField) != 0)
            {
                appendField(ref attributeFields, MemberIndexGroup <T> .Group.NonPublicFields);
            }
            if (attribute.IsAnonymousFields)
            {
                appendField(ref attributeFields, MemberIndexGroup <T> .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))
                {
                    BinarySerializeMemberAttribute memberAttribute = field.GetAttribute <BinarySerializeMemberAttribute>(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 <T> > deSerializeVersionFields = new LeftArray <DeSerializeVersionFields <T> >(0);
            LeftArray <DeSerializeVersionField>       attributeVersionFields   = new LeftArray <DeSerializeVersionField>(attributeFields.Length);
            foreach (DeSerializeVersionField field in attributeFields.Sort(DeSerializeVersionField.GlobalVersionSort))
            {
                if (field.GlobalVersion != globalVersion)
                {
                    deSerializeVersionFields.Add(new DeSerializeVersionFields <T>(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 <T>(globalVersion, attributeVersionFields.GetArray(), removeMemberCount, noSerializeMemberCount).CreateOnly(attribute);
            if (deSerializeVersionFields.Length != 0)
            {
                int count = deSerializeVersionFields.Length;
                DeSerializeVersionFields <T>[] deSerializeVersionFieldsArray = new DeSerializeVersionFields <T> [count];
                foreach (DeSerializeVersionFields <T> value in deSerializeVersionFields)
                {
                    deSerializeVersionFieldsArray[--count]          = value;
                    deSerializeVersionFieldsArray[count].CreateLock = new object();
                }
                fieldDeSerializers = deSerializeVersionFieldsArray;
            }
        }
Пример #7
0
        /// <summary>
        /// 未知类型枚举序列化委托调用函数信息
        /// </summary>
        /// <param name="type">数组类型</param>
        /// <returns>未知类型序列化委托调用函数信息</returns>
        public static MethodInfo GetMember(Type type)
        {
            MethodInfo method;

            if (memberMethods.TryGetValue(type, out method))
            {
                return(method);
            }
            if (type.IsArray)
            {
                Type elementType = type.GetElementType();
                if (elementType.IsValueType)
                {
                    if (elementType.IsEnum)
                    {
                        method = EnumGenericType.Get(elementType).BinarySerializeEnumArrayMemberDelegate.Method;
                    }
                    else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        //method = nullableArrayMemberMethod.MakeGenericMethod(elementType.GetGenericArguments());
                        method = StructGenericType.Get(elementType.GetGenericArguments()[0]).BinarySerializeNullableArrayMemberMethod;
                    }
                    //else method = structArrayMemberMethod.MakeGenericMethod(elementType);
                    else
                    {
                        method = GenericType.Get(elementType).BinarySerializeStructArrayMemberMethod;
                    }
                }
                //else method = arrayMemberMethod.MakeGenericMethod(elementType);
                else
                {
                    method = ClassGenericType.Get(elementType).BinarySerializeArrayMemberMethod;
                }
            }
            else if (type.IsEnum)
            {
                method = (EnumGenericType.Get(type).BinarySerializeEnumMemberDelegate).Method;
            }
            else
            {
                if (type.IsGenericType)
                {
                    Type genericType = type.GetGenericTypeDefinition();
                    if (genericType == typeof(Dictionary <,>) || genericType == typeof(SortedDictionary <,>) || genericType == typeof(SortedList <,>))
                    {
                        Type[] parameterTypes = type.GetGenericArguments();
                        //method = dictionaryMemberMethod.MakeGenericMethod(type, parameterTypes[0], parameterTypes[1]);
                        method = DictionaryGenericType3.Get(type, parameterTypes[0], parameterTypes[1]).BinarySerializeDictionaryMemberMethod;
                    }
                    else if (genericType == typeof(Nullable <>))
                    {
                        //method = nullableMemberSerializeMethod.MakeGenericMethod(type.GetGenericArguments());
                        method = StructGenericType.Get(type.GetGenericArguments()[0]).BinarySerializeNullableMemberMethod;
                    }
                    else if (genericType == typeof(KeyValuePair <,>))
                    {
                        //method = KeyValuePairSerializeMethod.MakeGenericMethod(type.GetGenericArguments());
                        method = GenericType2.Get(type.GetGenericArguments()).BinarySerializeKeyValuePairMethod.Method;
                    }
                }
                if (method == null)
                {
                    //if (type.IsValueType) method = structSerializeMethod.MakeGenericMethod(type);
                    //else method = memberClassSerializeMethod.MakeGenericMethod(type);
                    if (type.IsValueType)
                    {
                        method = StructGenericType.Get(type).BinarySerializeStructMethod;
                    }
                    else
                    {
                        method = ClassGenericType.Get(type).BinarySerializeMemberClassMethod;
                    }
                }
            }
            memberMethods.Set(type, method);
            return(method);
        }