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())); } else { defaultSerializer = ignore; } isValueType = true; return; } if (type.IsEnum) { defaultSerializer = enumToString; isValueType = true; return; } if (type.IsPointer || typeof(Delegate).IsAssignableFrom(type)) { defaultSerializer = ignore; isValueType = true; return; } if (type.IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); if (genericType == typeof(Nullable <>)) { defaultSerializer = (Action <Serializer, valueType>)Delegate.CreateDelegate(typeof(Action <Serializer, valueType>), SerializeMethodCache.GetNullable(type)); isValueType = true; return; } } if ((methodInfo = SerializeMethodCache.GetCustom(type)) != null) { if (type.IsValueType) { #if NOJIT defaultSerializer = new CustomSerializer(methodInfo).Serialize; #else DynamicMethod dynamicMethod = new DynamicMethod("CustomXmlSerializer", 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.DefaultAttribute); 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; } } } LeftArray <KeyValue <FieldIndex, MemberAttribute> > fields = SerializeMethodCache.GetFields(MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), attribute); LeftArray <PropertyMethod> 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.IsXml) { isBox = true; } } #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 (KeyValue <FieldIndex, MemberAttribute> member in fields) { if (isBox) { dynamicMethod.PushBox(member.Key); } else { dynamicMethod.Push(member.Key, member.Value); memberMapDynamicMethod.Push(member.Key, member.Value); } } foreach (PropertyMethod member in properties) { if (isBox) { dynamicMethod.PushBox(member.Property, member.Method); } else { dynamicMethod.Push(member.Property, member.Method, member.Attribute); memberMapDynamicMethod.Push(member.Property, member.Method, member.Attribute); } } memberSerializer = (Action <Serializer, valueType>)dynamicMethod.Create <Action <Serializer, valueType> >(); if (isBox) { defaultSerializer = memberSerializer; } else { memberMapSerializer = (Action <MemberMap, Serializer, valueType>)memberMapDynamicMethod.Create <Action <MemberMap, Serializer, valueType> >(); } #endif } }