///// <summary> ///// 字典转换函数信息 ///// </summary> //private static readonly MethodInfo structStructEnumerableMethod = typeof(Serializer).GetMethod("structStructEnumerable", BindingFlags.Instance | BindingFlags.NonPublic); ///// <summary> ///// 字典转换函数信息 ///// </summary> //private static readonly MethodInfo structClassEnumerableMethod = typeof(Serializer).GetMethod("structClassEnumerable", BindingFlags.Instance | BindingFlags.NonPublic); ///// <summary> ///// 字典转换函数信息 ///// </summary> //private static readonly MethodInfo classStructEnumerableMethod = typeof(Serializer).GetMethod("classStructEnumerable", BindingFlags.Instance | BindingFlags.NonPublic); ///// <summary> ///// 字典转换函数信息 ///// </summary> //private static readonly MethodInfo classClassEnumerableMethod = typeof(Serializer).GetMethod("classClassEnumerable", BindingFlags.Instance | BindingFlags.NonPublic); /// <summary> /// 获取枚举集合转换委托调用函数信息 /// </summary> /// <param name="type">枚举类型</param> /// <returns>枚举集合转换委托调用函数信息</returns> public static MethodInfo GetIEnumerable(Type type) { MethodInfo method; if (enumerableMethods.TryGetValue(type, out method)) { return(method); } foreach (Type interfaceType in type.GetInterfaces()) { if (interfaceType.IsGenericType) { Type genericType = interfaceType.GetGenericTypeDefinition(); if (genericType == typeof(IEnumerable <>)) { Type[] parameters = interfaceType.GetGenericArguments(); Type argumentType = parameters[0]; parameters[0] = typeof(IList <>).MakeGenericType(argumentType); ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { //method = (type.IsValueType ? (argumentType.IsValueType ? structStructEnumerableMethod : structClassEnumerableMethod) : (argumentType.IsValueType ? classStructEnumerableMethod : classClassEnumerableMethod)).MakeGenericMethod(type, argumentType); EnumerableGenericType2 EnumerableGenericType2 = EnumerableGenericType2.Get(type, argumentType); method = type.IsValueType ? (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeStructStructEnumerableMethod : EnumerableGenericType2.XmlSerializeStructClassEnumerableMethod) : (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeClassStructEnumerableMethod : EnumerableGenericType2.XmlSerializeClassClassEnumerableMethod); break; } parameters[0] = typeof(ICollection <>).MakeGenericType(argumentType); constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { //method = (type.IsValueType ? (argumentType.IsValueType ? structStructEnumerableMethod : structClassEnumerableMethod) : (argumentType.IsValueType ? classStructEnumerableMethod : classClassEnumerableMethod)).MakeGenericMethod(type, argumentType); EnumerableGenericType2 EnumerableGenericType2 = EnumerableGenericType2.Get(type, argumentType); method = type.IsValueType ? (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeStructStructEnumerableMethod : EnumerableGenericType2.XmlSerializeStructClassEnumerableMethod) : (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeClassStructEnumerableMethod : EnumerableGenericType2.XmlSerializeClassClassEnumerableMethod); break; } parameters[0] = typeof(IEnumerable <>).MakeGenericType(argumentType); constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { //method = (type.IsValueType ? (argumentType.IsValueType ? structStructEnumerableMethod : structClassEnumerableMethod) : (argumentType.IsValueType ? classStructEnumerableMethod : classClassEnumerableMethod)).MakeGenericMethod(type, argumentType); EnumerableGenericType2 EnumerableGenericType2 = EnumerableGenericType2.Get(type, argumentType); method = type.IsValueType ? (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeStructStructEnumerableMethod : EnumerableGenericType2.XmlSerializeStructClassEnumerableMethod) : (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeClassStructEnumerableMethod : EnumerableGenericType2.XmlSerializeClassClassEnumerableMethod); break; } parameters[0] = argumentType.MakeArrayType(); constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { //method = (type.IsValueType ? (argumentType.IsValueType ? structStructEnumerableMethod : structClassEnumerableMethod) : (argumentType.IsValueType ? classStructEnumerableMethod : classClassEnumerableMethod)).MakeGenericMethod(type, argumentType); EnumerableGenericType2 EnumerableGenericType2 = EnumerableGenericType2.Get(type, argumentType); method = type.IsValueType ? (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeStructStructEnumerableMethod : EnumerableGenericType2.XmlSerializeStructClassEnumerableMethod) : (argumentType.IsValueType ? EnumerableGenericType2.XmlSerializeClassStructEnumerableMethod : EnumerableGenericType2.XmlSerializeClassClassEnumerableMethod); break; } } else if (genericType == typeof(IDictionary <,>)) { ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { interfaceType }, null); if (constructorInfo != null) { //method = (type.IsValueType ? structStructEnumerableMethod : classStructEnumerableMethod).MakeGenericMethod(type, typeof(KeyValuePair<,>).MakeGenericType(interfaceType.GetGenericArguments())); EnumerableGenericType2 EnumerableGenericType2 = EnumerableGenericType2.Get(type, typeof(KeyValuePair <,>).MakeGenericType(interfaceType.GetGenericArguments())); method = type.IsValueType ? EnumerableGenericType2.XmlSerializeStructStructEnumerableMethod : EnumerableGenericType2.XmlSerializeClassStructEnumerableMethod; break; } } } } enumerableMethods.Set(type, method); return(method); }
/// <summary> /// 获取枚举集合转换委托调用函数信息 /// </summary> /// <param name="type">枚举类型</param> /// <returns>枚举集合转换委托调用函数信息</returns> public static Delegate GetIEnumerable(Type type) { Delegate method; if (enumerableMethods.TryGetValue(type, out method)) { return(method); } foreach (Type interfaceType in type.GetInterfaces()) { if (interfaceType.IsGenericType) { Type genericType = interfaceType.GetGenericTypeDefinition(); if (genericType == typeof(IEnumerable <>)) { Type[] parameters = interfaceType.GetGenericArguments(); Type argumentType = parameters[0]; parameters[0] = typeof(IList <>).MakeGenericType(argumentType); ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { if (type.IsValueType) { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeStructEnumerableMethod; } else { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeEnumerableMethod; } break; } parameters[0] = typeof(ICollection <>).MakeGenericType(argumentType); constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { if (type.IsValueType) { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeStructEnumerableMethod; } else { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeEnumerableMethod; } break; } parameters[0] = typeof(IEnumerable <>).MakeGenericType(argumentType); constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { if (type.IsValueType) { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeStructEnumerableMethod; } else { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeEnumerableMethod; } break; } parameters[0] = argumentType.MakeArrayType(); constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null); if (constructorInfo != null) { if (type.IsValueType) { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeStructEnumerableMethod; } else { method = EnumerableGenericType2.Get(type, argumentType).JsonSerializeEnumerableMethod; } break; } } else if (genericType == typeof(IDictionary <,>)) { ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { interfaceType }, null); if (constructorInfo != null) { if (type.IsValueType) { method = EnumerableGenericType2.Get(type, GenericType2.Get(interfaceType.GetGenericArguments()).KeyValuePairType).JsonSerializeStructEnumerableMethod; } else { method = EnumerableGenericType2.Get(type, GenericType2.Get(interfaceType.GetGenericArguments()).KeyValuePairType).JsonSerializeEnumerableMethod; } break; } } } } enumerableMethods.Set(type, method); return(method); }