Ejemplo n.º 1
0
 /// <summary>
 /// 对象序列化
 /// </summary>
 /// <param name="serializer">二进制数据序列化</param>
 /// <param name="value">数据对象</param>
 internal static void ClassSerialize(BinarySerializer serializer, T value)
 {
     if (DefaultSerializer == null)
     {
         if (serializer.CheckPoint(value))
         {
             if (serializer.Config.IsRealType)
             {
                 Type type = value.GetType();
                 if (type != typeof(T))
                 {
                     //if (serializer.CheckPoint(value))
                     {
                         serializer.Stream.Write(BinarySerializer.RealTypeValue);
                         //SerializeMethodCache.GetRealSerializer(type)(serializer, value);
                         GenericType.Get(type).BinarySerializeRealTypeObjectDelegate(serializer, value);
                     }
                     return;
                 }
             }
             if (AutoCSer.Metadata.DefaultConstructor <T> .Type == DefaultConstructorType.None)
             {
                 serializer.Stream.Write(BinarySerializer.NullValue);
             }
             else
             {
                 MemberSerialize(serializer, ref value);
             }
         }
     }
     else
     {
         DefaultSerializer(serializer, value);
     }
 }
Ejemplo n.º 2
0
 public void Serialize(object value)
 {
     if (value == null)
     {
         CharStream.WriteJsonNull();
     }
     else if (value.GetType() == typeof(Node))
     {
         Serialize((Node)value);
     }
     else if (Config.IsObject)
     {
         Type type = value.GetType();
         if (type == typeof(object))
         {
             CharStream.WriteJsonObject();
         }
         //else SerializeMethodCache.GetObject(type)(this, value);
         else
         {
             GenericType.Get(type).JsonSerializeObjectDelegate(this, value);
         }
     }
     else
     {
         CharStream.WriteJsonObject();
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 真实类型
        /// </summary>
        /// <param name="deSerializer">二进制数据反序列化</param>
        /// <param name="value">数据对象</param>
        private static void realType(DeSerializer deSerializer, ref valueType value)
        {
            RemoteType remoteType = default(RemoteType);

            TypeDeSerializer <RemoteType> .GetDeSerializer(deSerializer.GlobalVersion).MemberDeSerialize(deSerializer, ref remoteType);

            if (deSerializer.State == DeSerializeState.Success)
            {
                Type type;
                if (remoteType.TryGet(out type))
                {
                    if (value == null || type.IsValueType)
                    {
                        //value = (valueType)DeSerializeMethodCache.GetRealDeSerializer(type)(deSerializer, value);
                        value = (valueType)GenericType.Get(type).BinaryDeSerializeRealTypeObjectDelegate(deSerializer, value);
                    }
                    //else DeSerializeMethodCache.GetRealDeSerializer(type)(deSerializer, value);
                    else
                    {
                        GenericType.Get(type).BinaryDeSerializeRealTypeObjectDelegate(deSerializer, value);
                    }
                }
                else
                {
                    deSerializer.State = DeSerializeState.RemoteTypeError;
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 对象序列化
 /// </summary>
 /// <param name="serializer">二进制数据序列化</param>
 /// <param name="value">数据对象</param>
 internal static void ClassSerialize(Serializer serializer, valueType value)
 {
     if (DefaultSerializer == null)
     {
         if (serializer.CheckPoint(value))
         {
             if (serializer.Config.IsRealType)
             {
                 Type type = value.GetType();
                 if (type != typeof(valueType))
                 {
                     if (serializer.CheckPoint(value))
                     {
                         serializer.Stream.Write(Serializer.RealTypeValue);
                         //SerializeMethodCache.GetRealSerializer(type)(serializer, value);
                         GenericType.Get(type).BinarySerializeRealTypeObjectDelegate(serializer, value);
                     }
                     return;
                 }
             }
             if (Emit.Constructor <valueType> .New == null)
             {
                 serializer.Stream.Write(Serializer.NullValue);
             }
             else
             {
                 MemberSerialize(serializer, ref value);
             }
         }
     }
     else
     {
         DefaultSerializer(serializer, value);
     }
 }
Ejemplo n.º 5
0
        ///// <summary>
        ///// 字典转换函数信息
        ///// </summary>
        //private static readonly MethodInfo classSerializeMethod = typeof(Serializer).GetMethod("classSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 字典转换函数信息
        ///// </summary>
        //private static readonly MethodInfo structSerializeMethod = typeof(Serializer).GetMethod("structSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
        /// <summary>
        /// 未知类型枚举转换委托调用函数信息
        /// </summary>
        /// <param name="type">数组类型</param>
        /// <returns>未知类型转换委托调用函数信息</returns>
        public static MethodInfo GetType(Type type)
        {
            MethodInfo method;

            if (typeMethods.TryGetValue(type, out method))
            {
                return(method);
            }
            if (type.IsValueType)
            {
                if (type.IsGenericType)
                {
                    Type genericType = type.GetGenericTypeDefinition();
                    //if (genericType == typeof(Nullable<>)) method = nullableSerializeMethod.MakeGenericMethod(type.GetGenericArguments());
                    if (genericType == typeof(Nullable <>))
                    {
                        method = StructGenericType.Get(type.GetGenericArguments()[0]).XmlSerializeNullableMethod;
                    }
                }
                //if (method == null) method = structSerializeMethod.MakeGenericMethod(type);
                if (method == null)
                {
                    method = GenericType.Get(type).XmlSerializeStructMethod;
                }
            }
            //else method = classSerializeMethod.MakeGenericMethod(type);
            else
            {
                method = method = GenericType.Get(type).XmlSerializeClassMethod;
            }
            typeMethods.Set(type, method);
            return(method);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取成员转换函数信息
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="isCustom">成员类型</param>
        /// <returns>成员转换函数信息</returns>
        internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom)
        {
            MethodInfo methodInfo = XmlSerializer.GetSerializeMethod(type);

            if (methodInfo != null)
            {
                return(methodInfo);
            }
            if (type.IsArray)
            {
                return(GetArray(type.GetElementType()).Method);
            }
            //if (type.IsEnum) return GetEnum(type);
            if (type.IsEnum)
            {
                return(GenericType.Get(type).XmlSerializeEnumToStringMethod);
            }
            if ((methodInfo = GetCustom(type)) != null)
            {
                isCustom = type.IsValueType;
                return(methodInfo);
            }
            Delegate enumerableDelegate = GetIEnumerable(type);

            return(enumerableDelegate != null?GetIEnumerable(type).Method : GetType(type));
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 未知类型枚举转换委托调用函数信息
 /// </summary>
 /// <param name="type">数组类型</param>
 /// <returns>未知类型转换委托调用函数信息</returns>
 public static MethodInfo GetType(Type type)
 {
     if (type.IsValueType)
     {
         return(GenericType.Get(type).JsonSerializeStructSerializeMethod);
     }
     return(GenericType.Get(type).JsonSerializeClassSerializeMethod);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 获取数组转换委托调用函数信息
 /// </summary>
 /// <param name="type">数组类型</param>
 /// <returns>数组转换委托调用函数信息</returns>
 public static Delegate GetArray(Type type)
 {
     if (type.IsValueType)
     {
         return(GenericType.Get(type).XmlSerializeStructArrayMethod);
     }
     return(GenericType.Get(type).XmlSerializeArrayMethod);
 }
Ejemplo n.º 9
0
 ///// <summary>
 ///// 是否支持循环引用处理
 ///// </summary>
 ///// <typeparam name="valueType"></typeparam>
 ///// <returns></returns>
 //private static bool isReferenceMember<valueType>()
 //{
 //    return TypeSerializer<valueType>.IsReferenceMember;
 //}
 ///// <summary>
 ///// 是否支持循环引用处理函数信息
 ///// </summary>
 //private static readonly MethodInfo isReferenceMemberMethod = typeof(SerializeMethodCache).GetMethod("isReferenceMember", BindingFlags.Static | BindingFlags.NonPublic);
 ///// <summary>
 ///// 是否支持循环引用处理集合
 ///// </summary>
 //private static readonly AutoCSer.Threading.LockDictionary<Type, bool> isReferenceMembers = new AutoCSer.Threading.LockDictionary<Type, bool>();
 /// <summary>
 /// 是否支持循环引用处理
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsReferenceMember(Type type)
 {
     return(GenericType.Get(type).BinarySerializeIsReferenceMember);
     //bool isReferenceMember;
     //if (isReferenceMembers.TryGetValue(type, out isReferenceMember)) return isReferenceMember;
     //isReferenceMembers.Set(type, isReferenceMember = (bool)isReferenceMemberMethod.MakeGenericMethod(type).Invoke(null, null));
     //return isReferenceMember;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 获取字典转换委托调用函数信息
 /// </summary>
 /// <param name="type">枚举类型</param>
 /// <returns>字典转换委托调用函数信息</returns>
 public static Delegate GetDictionary(Type type)
 {
     Type[] types = type.GetGenericArguments();
     if (types[0] == typeof(string))
     {
         return(GenericType.Get(types[1]).JsonSerializeStringDictionaryMethod);
     }
     return(GenericType2.Get(types).JsonSerializeDictionaryMethod);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 预编译类型
 /// </summary>
 /// <param name="types"></param>
 internal static void Compile(Type[] types)
 {
     foreach (Type type in types)
     {
         if (type != null)
         {
             GenericType.Get(type).JsonDeSerializeCompile();
         }
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// 获取成员转换函数信息
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="isCustom">成员类型</param>
        /// <returns>成员转换函数信息</returns>
        internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom)
        {
            MethodInfo methodInfo = JsonSerializer.GetSerializeMethod(type);

            if (methodInfo != null)
            {
                return(methodInfo);
            }
            if (type.IsArray)
            {
                if (type.GetArrayRank() == 1)
                {
                    Type elementType = type.GetElementType();
                    if (elementType.IsValueType && (!elementType.IsGenericType || elementType.GetGenericTypeDefinition() != typeof(Nullable <>)))
                    {
                        return(StructGenericType.Get(elementType).JsonSerializeStructArrayMethod.Method);
                    }
                    return(GenericType.Get(elementType).JsonSerializeArrayMethod.Method);
                }
                return(GenericType.Get(type).JsonSerializeNotSupportDelegate.Method);
            }
            if (type.IsEnum)
            {
                return(GenericType.Get(type).JsonSerializeEnumToStringMethod);
            }
            if (type.isSerializeNotSupport())
            {
                return(GenericType.Get(type).JsonSerializeNotSupportDelegate.Method);
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Dictionary <,>))
                {
                    return(GetDictionary(type).Method);
                }
                if (genericType == typeof(Nullable <>))
                {
                    return(StructGenericType.Get(type.GetGenericArguments()[0]).JsonSerializeNullableMethod.Method);
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).JsonSerializeKeyValuePairMethod.Method);
                }
            }
            if ((methodInfo = GetCustom(type)) != null)
            {
                isCustom = type.IsValueType;
                return(methodInfo);
            }
            Delegate enumerableDelegate = GetIEnumerable(type);

            return(enumerableDelegate != null ? enumerableDelegate.Method : GetType(type));
        }
Ejemplo n.º 13
0
 ///// <summary>
 ///// 枚举转换调用函数信息集合
 ///// </summary>
 //private static readonly AutoCSer.Threading.LockDictionary<Type, MethodInfo> enumMethods = new AutoCSer.Threading.LockDictionary<Type, MethodInfo>();
 ///// <summary>
 ///// 字典转换函数信息
 ///// </summary>
 //private static readonly MethodInfo enumToStringMethod = typeof(Serializer).GetMethod("EnumToString", BindingFlags.Instance | BindingFlags.NonPublic);
 ///// <summary>
 ///// 获取枚举转换委托调用函数信息
 ///// </summary>
 ///// <param name="type">数组类型</param>
 ///// <returns>枚举转换委托调用函数信息</returns>
 //public static MethodInfo GetEnum(Type type)
 //{
 //    MethodInfo method;
 //    if (enumMethods.TryGetValue(type, out method)) return method;
 //    enumMethods.Set(type, method = enumToStringMethod.MakeGenericMethod(type));
 //    return method;
 //}
 ///// <summary>
 ///// 未知类型转换调用函数信息集合
 ///// </summary>
 //private static readonly AutoCSer.Threading.LockDictionary<Type, MethodInfo> typeMethods = new AutoCSer.Threading.LockDictionary<Type, MethodInfo>();
 ///// <summary>
 ///// 字典转换函数信息
 ///// </summary>
 //private static readonly MethodInfo classSerializeMethod = typeof(Serializer).GetMethod("classSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
 ///// <summary>
 ///// 字典转换函数信息
 ///// </summary>
 //private static readonly MethodInfo structSerializeMethod = typeof(Serializer).GetMethod("structSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
 /// <summary>
 /// 未知类型枚举转换委托调用函数信息
 /// </summary>
 /// <param name="type">数组类型</param>
 /// <returns>未知类型转换委托调用函数信息</returns>
 public static MethodInfo GetType(Type type)
 {
     if (type.IsValueType)
     {
         return(GenericType.Get(type).JsonSerializeStructSerializeMethod);
     }
     return(GenericType.Get(type).JsonSerializeClassSerializeMethod);
     //MethodInfo method;
     //if (typeMethods.TryGetValue(type, out method)) return method;
     //typeMethods.Set(type, method = type.IsValueType ? structSerializeMethod.MakeGenericMethod(type) : classSerializeMethod.MakeGenericMethod(type));
     //return method;
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 获取成员转换函数信息
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="isCustom"></param>
        /// <returns>成员转换函数信息</returns>
        internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom)
        {
            MethodInfo methodInfo = Parser.GetParseMethod(type);

            if (methodInfo != null)
            {
                return(methodInfo);
            }
            //if (type.IsArray) return GetArray(type.GetElementType());
            if (type.IsArray)
            {
                return(GenericType.Get(type.GetElementType()).JsonParseArrayMethod);
            }
            if (type.IsEnum)
            {
                return(GetEnum(type));
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                //if (genericType == typeof(Dictionary<,>)) return GetDictionary(type);
                if (genericType == typeof(Dictionary <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).JsonParseDictionaryMethod);
                }
                if (genericType == typeof(Nullable <>))
                {
                    return(GetNullable(type));
                }
                //if (genericType == typeof(KeyValuePair<,>)) return GetKeyValuePair(type);
                if (genericType == typeof(KeyValuePair <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).JsonParseKeyValuePairMethod);
                }
            }
            if ((methodInfo = GetCustom(type)) != null)
            {
                isCustom = type.IsValueType;
                return(methodInfo);
            }
            //if (type.IsAbstract || type.IsInterface) return typeParser.GetNoConstructorParser(type);
            if ((methodInfo = GetIEnumerableConstructor(type)) != null)
            {
                return(methodInfo);
            }
            //if (type.IsValueType) return GetValueType(type);
            //return GetType(type);
            if (type.IsValueType)
            {
                return(StructGenericType.Get(type).JsonParseStructMethod);
            }
            return(GenericType.Get(type).JsonParseTypeMethod);
        }
Ejemplo n.º 15
0
 ///// <summary>
 ///// 数组转换调用函数信息集合
 ///// </summary>
 //private static readonly AutoCSer.Threading.LockDictionary<Type, MethodInfo> arrayMethods = new AutoCSer.Threading.LockDictionary<Type, MethodInfo>();
 ///// <summary>
 ///// 字典转换函数信息
 ///// </summary>
 //private static readonly MethodInfo structArrayMethod = typeof(Serializer).GetMethod("structArray", BindingFlags.Instance | BindingFlags.NonPublic);
 ///// <summary>
 ///// 字典转换函数信息
 ///// </summary>
 //private static readonly MethodInfo arrayMethod = typeof(Serializer).GetMethod("array", BindingFlags.Instance | BindingFlags.NonPublic);
 /// <summary>
 /// 获取数组转换委托调用函数信息
 /// </summary>
 /// <param name="type">数组类型</param>
 /// <returns>数组转换委托调用函数信息</returns>
 public static MethodInfo GetArray(Type type)
 {
     if (type.IsValueType)
     {
         return(GenericType.Get(type).XmlSerializeStructArrayMethod);
     }
     return(GenericType.Get(type).XmlSerializeArrayMethod);
     //MethodInfo method;
     //if (arrayMethods.TryGetValue(type, out method)) return method;
     //arrayMethods.Set(type, method = (type.IsValueType ? structArrayMethod : arrayMethod).MakeGenericMethod(type));
     //return method;
 }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        ///// <summary>
        ///// 引用类型解析调用函数信息集合
        ///// </summary>
        //private static readonly AutoCSer.Threading.LockDictionary<Type, MethodInfo> typeMethods = new AutoCSer.Threading.LockDictionary<Type, MethodInfo>();
        ///// <summary>
        ///// 引用类型对象解析函数信息
        ///// </summary>
        //private static readonly MethodInfo typeParseMethod = typeof(Parser).GetMethod("typeParse", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 获取引用类型解析调用函数信息
        ///// </summary>
        ///// <param name="type">数据类型</param>
        ///// <returns>引用类型解析调用函数信息</returns>
        //public static MethodInfo GetType(Type type)
        //{
        //    MethodInfo method;
        //    if (typeMethods.TryGetValue(type, out method)) return method;
        //    //if (type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, nullValue<Type>.Array, null) == null) method = noConstructorMethod.MakeGenericMethod(type);
        //    //else
        //    method = typeParseMethod.MakeGenericMethod(type);
        //    typeMethods.Set(type, method);
        //    return method;
        //}
        /// <summary>
        /// 获取成员转换函数信息
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="isCustom"></param>
        /// <returns>成员转换函数信息</returns>
        internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom)
        {
            MethodInfo methodInfo = Parser.GetParseMethod(type);

            if (methodInfo != null)
            {
                return(methodInfo);
            }
            //if (type.IsArray) return GetArray(type.GetElementType());
            if (type.IsArray)
            {
                return(GenericType.Get(type.GetElementType()).XmlParseArrayMethod);
            }
            if (type.IsEnum)
            {
                return(GetEnum(type));
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Nullable <>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    //return parameterTypes[0].IsEnum ? GetNullableEnumParse(type, parameterTypes) : GetNullableParse(type, parameterTypes);
                    return(parameterTypes[0].IsEnum ? StructGenericType.Get(parameterTypes[0]).XmlParseNullableEnumMethod : StructGenericType.Get(parameterTypes[0]).XmlParseNullableMethod);
                }
                //if (genericType == typeof(KeyValuePair<,>)) return GetKeyValuePair(type);
                if (genericType == typeof(KeyValuePair <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).XmlParseKeyValuePairMethod);
                }
            }
            if ((methodInfo = GetCustom(type)) != null)
            {
                isCustom = type.IsValueType;
                return(methodInfo);
            }
            //if (type.IsAbstract || type.IsInterface) return typeParser.GetNoConstructorParser(type);
            if ((methodInfo = GetIEnumerableConstructor(type)) != null)
            {
                return(methodInfo);
            }
            if (type.IsValueType)
            {
                return(GetValueType(type));
            }
            //return GetType(type);
            return(GenericType.Get(type).XmlParseTypeMethod);
        }
Ejemplo n.º 18
0
        private void BuildConstructor(GenericTemplate classType, System.Reflection.ConstructorInfo constructorInfo)
        {
            var args = constructorInfo.GetParameters().Select(
                x => new VarInfo(
                    x.Name,
                    x.ParameterType.FullName == null?
                    GenericType.Get(x.ParameterType.GenericParameterPosition):
                    storage.GetType(ConvertMSILNames(x.ParameterType))
                    )).ToList();

            if (args.All(x => x.Type != null))
            {
                classType.AddConstructor(args);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 获取成员转换函数信息
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="isCustom">成员类型</param>
        /// <returns>成员转换函数信息</returns>
        internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom)
        {
            MethodInfo methodInfo = Serializer.GetSerializeMethod(type);

            if (methodInfo != null)
            {
                return(methodInfo);
            }
            //if (type.IsArray) return GetArray(type.GetElementType());
            if (type.IsArray)
            {
                Type elementType = type.GetElementType();
                if (elementType.IsValueType && (!elementType.IsGenericType || elementType.GetGenericTypeDefinition() != typeof(Nullable <>)))
                {
                    return(StructGenericType.Get(elementType).JsonSerializeStructArrayMethod);
                }
                return(GenericType.Get(elementType).JsonSerializeArrayMethod);
            }
            //if (type.IsEnum) return GetEnum(type);
            if (type.IsEnum)
            {
                return(GenericType.Get(type).JsonSerializeEnumToStringMethod);
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Dictionary <,>))
                {
                    return(GetDictionary(type));
                }
                //if (genericType == typeof(Nullable<>)) return GetNullable(type);
                if (genericType == typeof(Nullable <>))
                {
                    return(StructGenericType.Get(type.GetGenericArguments()[0]).JsonSerializeNullableMethod);
                }
                //if (genericType == typeof(KeyValuePair<,>)) return GetKeyValuePair(type);
                if (genericType == typeof(KeyValuePair <,>))
                {
                    return(GenericType2.Get(type.GetGenericArguments()).JsonSerializeKeyValuePairMethod);
                }
            }
            if ((methodInfo = GetCustom(type)) != null)
            {
                isCustom = type.IsValueType;
                return(methodInfo);
            }
            return(GetIEnumerable(type) ?? GetType(type));
        }
Ejemplo n.º 20
0
        ///// <summary>
        ///// object转换调用委托信息集合
        ///// </summary>
        //private static readonly AutoCSer.Threading.LockDictionary<Type, Action<Serializer, object>> objectMethods = new AutoCSer.Threading.LockDictionary<Type, Action<Serializer, object>>();
        ///// <summary>
        ///// 字典转换函数信息
        ///// </summary>
        //private static readonly MethodInfo serializeObjectMethod = typeof(Serializer).GetMethod("serializeObject", BindingFlags.Static | BindingFlags.NonPublic);
        ///// <summary>
        ///// 获取object转换调用委托信息
        ///// </summary>
        ///// <param name="type">真实类型</param>
        ///// <returns>object转换调用委托信息</returns>
        //public static Action<Serializer, object> GetObject(Type type)
        //{
        //    Action<Serializer, object> method;
        //    if (objectMethods.TryGetValue(type, out method)) return method;
        //    method = (Action<Serializer, object>)Delegate.CreateDelegate(typeof(Action<Serializer, object>), serializeObjectMethod.MakeGenericMethod(type));
        //    objectMethods.Set(type, method);
        //    return method;
        //}
        ///// <summary>
        ///// 数组转换调用函数信息集合
        ///// </summary>
        //private static readonly AutoCSer.Threading.LockDictionary<Type, MethodInfo> arrayMethods = new AutoCSer.Threading.LockDictionary<Type, MethodInfo>();
        ///// <summary>
        ///// 字典转换函数信息
        ///// </summary>
        //private static readonly MethodInfo arrayMethod = typeof(Serializer).GetMethod("array", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 获取数组转换委托调用函数信息
        ///// </summary>
        ///// <param name="type">数组类型</param>
        ///// <returns>数组转换委托调用函数信息</returns>
        //public static MethodInfo GetArray(Type type)
        //{
        //    MethodInfo method;
        //    if (arrayMethods.TryGetValue(type, out method)) return method;
        //    arrayMethods.Set(type, method = arrayMethod.MakeGenericMethod(type));
        //    return method;
        //}
        ///// <summary>
        ///// 字典转换调用函数信息集合
        ///// </summary>
        //private static readonly AutoCSer.Threading.LockDictionary<Type, MethodInfo> dictionaryMethods = new AutoCSer.Threading.LockDictionary<Type, MethodInfo>();
        ///// <summary>
        ///// 字典转换函数信息
        ///// </summary>
        //private static readonly MethodInfo dictionaryMethod = typeof(Serializer).GetMethod("dictionary", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 字符串字典转换函数信息
        ///// </summary>
        //private static readonly MethodInfo stringDictionaryMethod = typeof(Serializer).GetMethod("stringDictionary", BindingFlags.Instance | BindingFlags.NonPublic);
        /// <summary>
        /// 获取字典转换委托调用函数信息
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns>字典转换委托调用函数信息</returns>
        public static MethodInfo GetDictionary(Type type)
        {
            Type[] types = type.GetGenericArguments();
            if (types[0] == typeof(string))
            {
                return(GenericType.Get(types[1]).JsonSerializeStringDictionaryMethod);
            }
            return(GenericType2.Get(types).JsonSerializeDictionaryMethod);

            //MethodInfo method;
            //if (dictionaryMethods.TryGetValue(type, out method)) return method;
            //Type[] types = type.GetGenericArguments();
            //if (types[0] == typeof(string)) method = stringDictionaryMethod.MakeGenericMethod(types[1]);
            //else method = dictionaryMethod.MakeGenericMethod(types);
            //dictionaryMethods.Set(type, method);
            //return method;
        }
Ejemplo n.º 21
0
 /// <summary>
 /// 找不到构造函数
 /// </summary>
 /// <param name="serializer">二进制数据序列化</param>
 /// <param name="value">数据对象</param>
 private static void noConstructor(Serializer serializer, valueType value)
 {
     if (serializer.CheckPoint(value))
     {
         if (serializer.Config.IsRealType)
         {
             Type type = value.GetType();
             if (type == typeof(valueType))
             {
                 serializer.Stream.Write(Serializer.NullValue);
             }
             //else SerializeMethodCache.GetRealSerializer(type)(serializer, value);
             else
             {
                 GenericType.Get(type).BinarySerializeRealTypeObjectDelegate(serializer, value);
             }
         }
         else
         {
             serializer.Stream.Write(Serializer.NullValue);
         }
     }
 }
Ejemplo n.º 22
0
        private void BuildMethod(GenericTemplate classType, System.Reflection.MethodInfo type)
        {
            var list = new List <VarInfo>();

            foreach (var item in type.GetParameters())
            {
                if (item.ParameterType.FullName == null)
                {
                    list.Add(new VarInfo(item.Name, GenericType.Get(item.ParameterType.GenericParameterPosition)));
                }
                else
                {
                    var itemType = storage.GetType(ConvertMSILNames(item.ParameterType));
                    list.Add(new VarInfo(item.Name, itemType));
                }
            }

            var retType = type.ReturnType.FullName == null
                ? GenericType.Get(type.ReturnType.GenericParameterPosition)
                : storage.GetType(ConvertMSILNames(type.ReturnType));

            classType.AddMethod(type.Name, list, retType, false);
        }
Ejemplo n.º 23
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 24
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);
                }
            }
        }
Ejemplo n.º 25
0
        static TypeSerializer()
        {
            Type       type       = typeof(valueType);
            MethodInfo methodInfo = Serializer.GetSerializeMethod(type);

            if (methodInfo != null)
            {
                defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                isValueType       = true;
                return;
            }
            if (type.IsArray)
            {
                //if (type.GetArrayRank() == 1) defaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.GetArray(type.GetElementType()));
                if (type.GetArrayRank() == 1)
                {
                    Type elementType = type.GetElementType();
                    if (elementType.IsValueType && (!elementType.IsGenericType || elementType.GetGenericTypeDefinition() != typeof(Nullable <>)))
                    {
                        defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), StructGenericType.Get(elementType).JsonSerializeStructArrayMethod);
                    }
                    else
                    {
                        defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), GenericType.Get(elementType).JsonSerializeArrayMethod);
                    }
                }
                else
                {
                    defaultSerializer = arrayManyRank;
                }
                isValueType = true;
                return;
            }
            if (type.IsEnum)
            {
                defaultSerializer = enumToString;
                isValueType       = true;
                return;
            }
            if (type.IsInterface || type.IsPointer || typeof(Delegate).IsAssignableFrom(type))
            {
                defaultSerializer = toNull;
                isValueType       = true;
                return;
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Dictionary <,>))
                {
                    defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), SerializeMethodCache.GetDictionary(type));
                    isValueType       = true;
                    return;
                }
                if (genericType == typeof(Nullable <>))
                {
                    //defaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.GetNullable(type));
                    defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), StructGenericType.Get(type.GetGenericArguments()[0]).JsonSerializeNullableMethod);
                    isValueType       = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    //defaultSerializer = (Action<Serializer, valueType>)Delegate.CreateDelegate(typeof(Action<Serializer, valueType>), SerializeMethodCache.GetKeyValuePair(type));
                    defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), GenericType2.Get(type.GetGenericArguments()).JsonSerializeKeyValuePairMethod);
                    isValueType       = true;
                    return;
                }
            }
            if ((methodInfo = SerializeMethodCache.GetCustom(type)) != null)
            {
                if (type.IsValueType)
                {
#if NOJIT
                    defaultSerializer = new CustomSerializer(methodInfo).Serialize;
#else
                    DynamicMethod dynamicMethod = new DynamicMethod("CustomJsonSerializer", 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);
                }
                isValueType = true;
            }
            else if ((methodInfo = SerializeMethodCache.GetIEnumerable(type)) != null)
            {
                defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), methodInfo);
                isValueType       = true;
            }
            else
            {
                Type attributeType;
                attribute = type.customAttribute <SerializeAttribute>(out attributeType) ?? (type.Name[0] == '<' ? SerializeAttribute.AnonymousTypeMember : Serializer.AllMemberAttribute);
                if (type.IsValueType)
                {
                    isValueType = true;
                }
                else if (attribute != Serializer.AllMemberAttribute && 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;
                        }
                    }
                }
                LeftArray <FieldIndex> fields = SerializeMethodCache.GetFields(MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), attribute);
                LeftArray <KeyValue <PropertyIndex, MethodInfo> > properties = SerializeMethodCache.GetProperties(MemberIndexGroup <valueType> .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;
                    }
                }
#if AutoCSer
                AutoCSer.WebView.ClientTypeAttribute clientType = isBox ? null : AutoCSer.Metadata.TypeAttribute.GetAttribute <AutoCSer.WebView.ClientTypeAttribute>(type);
                if (clientType != null)
                {
                    if (clientType.MemberName == null)
                    {
                        viewClientTypeName = "new " + clientType.GetClientName(type) + "({";
                    }
                    else
                    {
                        viewClientTypeName = clientType.GetClientName(type) + ".Get({";
                    }
                }
#endif
#if NOJIT
                if (isBox)
                {
                    defaultSerializer = memberSerializer = new FieldPropertySerializer(ref fields, ref properties).SerializeBox;
                }
                else
                {
                    memberSerializer    = new FieldPropertySerializer(ref fields, ref properties).Serialize;
                    memberMapSerializer = new MemberMapSerializer(ref fields, ref properties).Serialize;
                }
#else
                SerializeMemberDynamicMethod    dynamicMethod          = new SerializeMemberDynamicMethod(type);
                SerializeMemberMapDynamicMethod memberMapDynamicMethod = isBox ? default(SerializeMemberMapDynamicMethod) : new SerializeMemberMapDynamicMethod(type);
                foreach (FieldIndex member in fields)
                {
                    if (isBox)
                    {
                        dynamicMethod.PushBox(member);
                    }
                    else
                    {
                        dynamicMethod.Push(member);
                        memberMapDynamicMethod.Push(member);
                    }
                }
                foreach (KeyValue <PropertyIndex, MethodInfo> member in properties)
                {
                    if (isBox)
                    {
                        dynamicMethod.PushBox(member.Key, member.Value);
                    }
                    else
                    {
                        dynamicMethod.Push(member.Key, member.Value);
                        memberMapDynamicMethod.Push(member.Key, member.Value);
                    }
                }
                memberSerializer = (Action <Serializer, valueType>)dynamicMethod.Create <Action <Serializer, valueType> >();
                if (isBox)
                {
                    defaultSerializer = memberSerializer;
                }
                else
                {
                    memberMapSerializer = (Action <MemberMap, Serializer, valueType, CharStream>)memberMapDynamicMethod.Create <Action <MemberMap, Serializer, valueType, CharStream> >();
                }
#endif
            }
        }
Ejemplo n.º 26
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 = GenericType.Get(elementType).BinaryDeSerializeEnumUIntArrayMethod;                          // DeSerializeMethodCache.EnumUIntArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(byte))
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumByteArrayMethod;                               // DeSerializeMethodCache.EnumByteArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(ulong))
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumULongArrayMethod;                                // DeSerializeMethodCache.EnumULongArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(ushort))
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumUShortArrayMethod;                                 // DeSerializeMethodCache.EnumUShortArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(long))
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumLongArrayMethod;                               // DeSerializeMethodCache.EnumLongArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(short))
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumShortArrayMethod;                                // DeSerializeMethodCache.EnumShortArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(sbyte))
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumSByteArrayMethod;                                // DeSerializeMethodCache.EnumSByteArrayMethod.MakeGenericMethod(elementType);
                                }
                                else
                                {
                                    methodInfo = GenericType.Get(elementType).BinaryDeSerializeEnumIntArrayMethod; // DeSerializeMethodCache.EnumIntArrayMethod.MakeGenericMethod(elementType);
                                }
                                IsReferenceMember = false;
                            }
                            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                //methodInfo = DeSerializeMethodCache.NullableArrayMethod.MakeGenericMethod(elementType = elementType.GetGenericArguments()[0]);
                                methodInfo        = StructGenericType.Get(elementType = elementType.GetGenericArguments()[0]).BinaryDeSerializeNullableArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                            else
                            {
                                //methodInfo = DeSerializeMethodCache.StructArrayMethod.MakeGenericMethod(elementType);
                                methodInfo        = GenericType.Get(elementType).BinaryDeSerializeStructArrayMethod;
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                        }
                        else
                        {
                            //methodInfo = DeSerializeMethodCache.ArrayMethod.MakeGenericMethod(elementType);
                            methodInfo        = ClassGenericType.Get(elementType).BinaryDeSerializeArrayMethod;
                            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()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), GenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeLeftArrayMethod);
                    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()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), GenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeSubArrayMethod);
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#endif
                if (genericType == typeof(Dictionary <,>))
                {
                    //DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.DictionaryDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeDictionaryMethod);
                    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()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), StructGenericType.Get(type.GetGenericArguments()[0]).BinaryDeSerializeNullableMethod);
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    //DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.KeyValuePairDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeKeyValuePairMethod);
                    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()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), GenericType2.Get(type.GetGenericArguments()).BinaryDeSerializeSortedDictionaryMethod);
                    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()));
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), 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(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);
                            if (type.IsValueType)
                            {
                                methodInfo = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                methodInfo = 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)
                        {
                            //methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            if (type.IsValueType)
                            {
                                methodInfo = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                methodInfo = 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)
                        {
                            //methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            if (type.IsValueType)
                            {
                                methodInfo = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                methodInfo = 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)
                        {
                            //methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            if (type.IsValueType)
                            {
                                methodInfo = CollectionGenericType2.Get(type, argumentType).BinaryDeSerializeStructCollectionMethod;
                            }
                            else
                            {
                                methodInfo = 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();
                            //methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructDictionaryDeSerializeMethod : DeSerializeMethodCache.ClassDictionaryDeSerializeMethod).MakeGenericMethod(type, parameters[0], parameters[1]);
                            if (type.IsValueType)
                            {
                                methodInfo = DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinaryDeSerializeStructDictionaryMethod;
                            }
                            else
                            {
                                methodInfo = DictionaryGenericType3.Get(type, parameters[0], parameters[1]).BinaryDeSerializeClassDictionaryMethod;
                            }
                            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;
            }
        }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
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;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 29
0
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumByteArrayMemberMethod = typeof(Serializer).GetMethod("enumByteArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumSByteArrayMemberMethod = typeof(Serializer).GetMethod("enumSByteArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumShortArrayMemberMethod = typeof(Serializer).GetMethod("enumShortArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUShortArrayMemberMethod = typeof(Serializer).GetMethod("enumUShortArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumIntArrayMemberMethod = typeof(Serializer).GetMethod("enumIntArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUIntArrayMemberMethod = typeof(Serializer).GetMethod("enumUIntArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumLongArrayMemberMethod = typeof(Serializer).GetMethod("enumLongArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumULongArrayMemberMethod = typeof(Serializer).GetMethod("enumULongArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo nullableArrayMemberMethod = typeof(Serializer).GetMethod("nullableArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo structArrayMemberMethod = typeof(Serializer).GetMethod("structArrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 数组转换函数信息
        ///// </summary>
        //private static readonly MethodInfo arrayMemberMethod = typeof(Serializer).GetMethod("arrayMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumByteMemberMethod = typeof(Serializer).GetMethod("enumByteMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumSByteMemberMethod = typeof(Serializer).GetMethod("enumSByteMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumShortMemberMethod = typeof(Serializer).GetMethod("enumShortMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUShortMemberMethod = typeof(Serializer).GetMethod("enumUShortMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumIntMemberMethod = typeof(Serializer).GetMethod("enumIntMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUIntMemberMethod = typeof(Serializer).GetMethod("enumUIntMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumLongMemberMethod = typeof(Serializer).GetMethod("enumLongMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举转换函数信息
        ///// </summary>
        //private static readonly MethodInfo enumULongMemberMethod = typeof(Serializer).GetMethod("enumULongMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 字典序列化函数信息
        ///// </summary>
        //private static readonly MethodInfo dictionaryMemberMethod = typeof(Serializer).GetMethod("dictionaryMember", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 对象序列化函数信息
        ///// </summary>
        //private static readonly MethodInfo nullableMemberSerializeMethod = typeof(Serializer).GetMethod("nullableMemberSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 对象序列化函数信息
        ///// </summary>
        //private static readonly MethodInfo structSerializeMethod = typeof(Serializer).GetMethod("structSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 引用类型成员序列化函数信息
        ///// </summary>
        //private static readonly MethodInfo memberClassSerializeMethod = typeof(Serializer).GetMethod("MemberClassSerialize", BindingFlags.Instance | BindingFlags.NonPublic);
        /// <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)
                    {
                        Type enumType = System.Enum.GetUnderlyingType(elementType);
                        if (enumType == typeof(uint))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumUIntArrayMemberMethod;                          // enumUIntArrayMemberMethod;
                        }
                        else if (enumType == typeof(byte))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumByteArrayMemberMethod;                               // enumByteArrayMemberMethod;
                        }
                        else if (enumType == typeof(ulong))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumULongArrayMemberMethod;                                // enumULongArrayMemberMethod;
                        }
                        else if (enumType == typeof(ushort))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumUShortArrayMemberMethod;                                 // enumUShortArrayMemberMethod;
                        }
                        else if (enumType == typeof(long))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumLongArrayMemberMethod;                               // enumLongArrayMemberMethod;
                        }
                        else if (enumType == typeof(short))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumShortArrayMemberMethod;                                // enumShortArrayMemberMethod;
                        }
                        else if (enumType == typeof(sbyte))
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumSByteArrayMemberMethod;                                // enumSByteArrayMemberMethod;
                        }
                        else
                        {
                            method = GenericType.Get(elementType).BinarySerializeEnumIntArrayMemberMethod; // enumIntArrayMemberMethod;
                        }
                        //method = method.MakeGenericMethod(elementType);
                    }
                    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)
            {
                Type enumType = System.Enum.GetUnderlyingType(type);
                if (enumType == typeof(uint))
                {
                    method = GenericType.Get(type).BinarySerializeEnumUIntMemberMethod;                          // enumUIntMemberMethod;
                }
                else if (enumType == typeof(byte))
                {
                    method = GenericType.Get(type).BinarySerializeEnumByteMemberMethod;                               // enumByteMemberMethod;
                }
                else if (enumType == typeof(ulong))
                {
                    method = GenericType.Get(type).BinarySerializeEnumULongMemberMethod;                                // enumULongMemberMethod;
                }
                else if (enumType == typeof(ushort))
                {
                    method = GenericType.Get(type).BinarySerializeEnumUShortMemberMethod;                                 // enumUShortMemberMethod;
                }
                else if (enumType == typeof(long))
                {
                    method = GenericType.Get(type).BinarySerializeEnumLongMemberMethod;                               // enumLongMemberMethod;
                }
                else if (enumType == typeof(short))
                {
                    method = GenericType.Get(type).BinarySerializeEnumShortMemberMethod;                                // enumShortMemberMethod;
                }
                else if (enumType == typeof(sbyte))
                {
                    method = GenericType.Get(type).BinarySerializeEnumSByteMemberMethod;                                // enumSByteMemberMethod;
                }
                else
                {
                    method = GenericType.Get(type).BinarySerializeEnumIntMemberMethod; // enumIntMemberMethod;
                }
                //method = method.MakeGenericMethod(type);
            }
            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;
                    }
                }
                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);
        }
Ejemplo n.º 30
0
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumByteMethod = typeof(Parser).GetMethod("enumByte", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumSByteMethod = typeof(Parser).GetMethod("enumSByte", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumShortMethod = typeof(Parser).GetMethod("enumShort", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUShortMethod = typeof(Parser).GetMethod("enumUShort", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumIntMethod = typeof(Parser).GetMethod("enumInt", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUIntMethod = typeof(Parser).GetMethod("enumUInt", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumLongMethod = typeof(Parser).GetMethod("enumLong", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumULongMethod = typeof(Parser).GetMethod("enumULong", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumByteFlagsMethod = typeof(Parser).GetMethod("enumByteFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumSByteFlagsMethod = typeof(Parser).GetMethod("enumSByteFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumShortFlagsMethod = typeof(Parser).GetMethod("enumShortFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUShortFlagsMethod = typeof(Parser).GetMethod("enumUShortFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumIntFlagsMethod = typeof(Parser).GetMethod("enumIntFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumUIntFlagsMethod = typeof(Parser).GetMethod("enumUIntFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumLongFlagsMethod = typeof(Parser).GetMethod("enumLongFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        ///// <summary>
        ///// 枚举值解析函数信息
        ///// </summary>
        //private static readonly MethodInfo enumULongFlagsMethod = typeof(Parser).GetMethod("enumULongFlags", BindingFlags.Instance | BindingFlags.NonPublic);
        /// <summary>
        /// 获取枚举解析调用函数信息
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns>枚举解析调用函数信息</returns>
        public static MethodInfo GetEnum(Type type)
        {
            MethodInfo method;

            if (enumMethods.TryGetValue(type, out method))
            {
                return(method);
            }
            Type enumType = System.Enum.GetUnderlyingType(type);

            if (AutoCSer.Metadata.TypeAttribute.GetAttribute <FlagsAttribute>(type) == null)
            {
                if (enumType == typeof(uint))
                {
                    method = GenericType.Get(type).JsonParseEnumUIntMethod;                          // enumUIntMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(byte))
                {
                    method = GenericType.Get(type).JsonParseEnumByteMethod;                               // enumByteMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(ulong))
                {
                    method = GenericType.Get(type).JsonParseEnumULongMethod;                                // enumULongMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(ushort))
                {
                    method = GenericType.Get(type).JsonParseEnumUShortMethod;                                 // enumUShortMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(long))
                {
                    method = GenericType.Get(type).JsonParseEnumLongMethod;                               // enumLongMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(short))
                {
                    method = GenericType.Get(type).JsonParseEnumShortMethod;                                // enumShortMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(sbyte))
                {
                    method = GenericType.Get(type).JsonParseEnumSByteMethod;                                // enumSByteMethod.MakeGenericMethod(type);
                }
                else
                {
                    method = GenericType.Get(type).JsonParseEnumIntMethod; // enumIntMethod.MakeGenericMethod(type);
                }
            }
            else
            {
                if (enumType == typeof(uint))
                {
                    method = GenericType.Get(type).JsonParseEnumUIntFlagsMethod;                          // enumUIntFlagsMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(byte))
                {
                    method = GenericType.Get(type).JsonParseEnumByteFlagsMethod;                               // enumByteFlagsMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(ulong))
                {
                    method = GenericType.Get(type).JsonParseEnumULongFlagsMethod;                                // enumULongFlagsMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(ushort))
                {
                    method = GenericType.Get(type).JsonParseEnumUShortFlagsMethod;                                 // enumUShortFlagsMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(long))
                {
                    method = GenericType.Get(type).JsonParseEnumLongFlagsMethod;                               // enumLongFlagsMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(short))
                {
                    method = GenericType.Get(type).JsonParseEnumShortFlagsMethod;                                // enumShortFlagsMethod.MakeGenericMethod(type);
                }
                else if (enumType == typeof(sbyte))
                {
                    method = GenericType.Get(type).JsonParseEnumSByteFlagsMethod;                                // enumSByteFlagsMethod.MakeGenericMethod(type);
                }
                else
                {
                    method = GenericType.Get(type).JsonParseEnumIntFlagsMethod; // enumIntFlagsMethod.MakeGenericMethod(type);
                }
            }
            enumMethods.Set(type, method);
            return(method);
        }