/// <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)); }
/// <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.IsEnum) { return(GetEnum(type)); } if (type.IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); if (genericType == typeof(Dictionary <,>)) { return(GetDictionary(type)); } if (genericType == typeof(Nullable <>)) { return(GetNullable(type)); } if (genericType == typeof(KeyValuePair <,>)) { return(GetKeyValuePair(type)); } } if ((methodInfo = GetCustom(type)) != null) { isCustom = type.IsValueType; return(methodInfo); } return(GetIEnumerable(type) ?? GetType(type)); }
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 } }