Exemplo n.º 1
0
        /// <summary>
        /// 获取字段成员集合
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="typeAttribute">类型配置</param>
        /// <returns>字段成员集合</returns>
        public static LeftArray <KeyValue <FieldIndex, MemberAttribute> > GetFields(FieldIndex[] fields, SerializeAttribute typeAttribute)
        {
            LeftArray <KeyValue <FieldIndex, MemberAttribute> > values = new LeftArray <KeyValue <FieldIndex, MemberAttribute> >(fields.Length);

            foreach (FieldIndex field in fields)
            {
                Type type = field.Member.FieldType;
                if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore && !typeof(Delegate).IsAssignableFrom(type) && !field.Member.IsDefined(typeof(IgnoreMemberAttribute), typeAttribute.IsBaseTypeAttribute))
                {
                    MemberAttribute attribute = field.GetAttribute <MemberAttribute>(typeAttribute.IsBaseTypeAttribute);
                    if (typeAttribute.IsAttribute ? (attribute != null && attribute.IsSetup) : (attribute == null || attribute.IsSetup))
                    {
                        values.Add(new KeyValue <FieldIndex, MemberAttribute>(field, attribute));
                    }
                }
            }
            return(values);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取属性成员集合
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="typeAttribute">类型配置</param>
        /// <returns>属性成员集合</returns>
        public static LeftArray <PropertyMethod> GetProperties(PropertyIndex[] properties, SerializeAttribute typeAttribute)
        {
            LeftArray <PropertyMethod> values = new LeftArray <PropertyMethod>(properties.Length);

            foreach (PropertyIndex property in properties)
            {
                if (property.Member.CanWrite)
                {
                    Type type = property.Member.PropertyType;
                    if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !property.IsIgnore && !typeof(Delegate).IsAssignableFrom(type) && !property.Member.IsDefined(typeof(IgnoreMemberAttribute), typeAttribute.IsBaseTypeAttribute))
                    {
                        MemberAttribute attribute = property.GetAttribute <MemberAttribute>(typeAttribute.IsBaseTypeAttribute);
                        if (typeAttribute.IsAttribute ? (attribute != null && attribute.IsSetup) : (attribute == null || attribute.IsSetup))
                        {
                            MethodInfo method = property.Member.GetSetMethod(true);
                            if (method != null && method.GetParameters().Length == 1)
                            {
                                values.Add(new PropertyMethod {
                                    Property = property, Method = method, Attribute = attribute
                                });
                            }
                        }
                    }
                }
            }
            return(values);
        }
Exemplo n.º 3
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()));
                }
                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
            }
        }