Exemple #1
0
        private List <MemberSerializationInfo> BuildMembers()
        {
            var memberInfos = new List <MemberSerializationInfo>();

            if (this.IsCollection) //类型是集合,因此类型本身也要加入序列化
            {
                memberInfos.Add(MemberSerializationInfo.Create(this.ClassType));
            }

            const BindingFlags memberFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            var members = this.ClassType.GetPropertyAndFields(memberFlags);

            foreach (MemberInfo member in members)
            {
                if (member is FieldInfo)
                {
                    DTOMemberAttribute attr = GetMemberAttribute(member);
                    if (attr == null)
                    {
                        continue;
                    }

                    memberInfos.Add(MemberSerializationInfo.Create(member, attr));
                }
                else
                {
                    var property = member as PropertyInfo;
                    if (property != null)
                    {
                        DTOMemberAttribute attr = GetMemberAttribute(member);
                        if (attr == null)
                        {
                            continue;
                        }

                        if (!(property.CanRead || property.CanWrite)) //因为有可能对象值需要写入或者只需要读取,因此只要有其中一种就可以
                        {
                            throw new SerializationException(string.Format("{0}.{1} 必须定义读取或写入的方法", this.ClassType.Name, property.Name));
                        }
                        memberInfos.Add(MemberSerializationInfo.Create(member, attr));
                    }
                }
            }

            OnBuildMembers(memberInfos);

            return(memberInfos);
        }
        public static MemberSerializationInfo Create(MemberInfo memberInfo, DTOMemberAttribute memberAttribute)
        {
            Type t = GetTargetType(null, memberInfo);

            //数组
            if (t.IsArray)
            {
                return(new ArraySerializationInfo(memberInfo, memberAttribute));
            }
            //ICollection或IDictionary
            MemberSerializationInfo info = CreateByCollection(t, memberInfo, memberAttribute);

            if (info != null)
            {
                return(info);
            }
            //普通类型
            return(new MemberSerializationInfo(memberInfo, memberAttribute));
        }
Exemple #3
0
        private List <MemberSerializationInfo> BuildMembers()
        {
            var memberInfos = new List <MemberSerializationInfo>();

            if (this.IsCollection) //类型是集合,因此类型本身也要加入序列化
            {
                memberInfos.Add(MemberSerializationInfo.Create(this.ClassType));
            }

            const BindingFlags memberFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            foreach (MemberInfo member in _classType.GetMembers(memberFlags))
            {
                DTOMemberAttribute attr = DTOMemberAttribute.GetAttribute(member);
                if (attr == null)
                {
                    continue;
                }

                if (member is FieldInfo)
                {
                    memberInfos.Add(MemberSerializationInfo.Create(member, attr));
                }
                else
                {
                    var property = member as PropertyInfo;
                    if (property != null)
                    {
                        if (!(property.CanRead && property.CanWrite))
                        {
                            throw new SerializationException(string.Format("{0}.{1} 必须定义读取和写入的方法", _classType.Name, property.Name));
                        }
                        memberInfos.Add(MemberSerializationInfo.Create(member, attr));
                    }
                }
            }

            memberInfos.Sort(MemberSerializationInfoComparator.Instance);

            return(memberInfos);
        }
Exemple #4
0
 public CollectionSerializationInfo(MemberInfo memberInfo
                                    , DTOMemberAttribute memberAttribute)
     : base(memberInfo, memberAttribute)
 {
 }
Exemple #5
0
 public ArraySerializationInfo(MemberInfo memberInfo
                               , DTOMemberAttribute memberAttribute)
     : base(memberInfo, memberAttribute)
 {
 }
 protected override DTOMemberAttribute GetMemberAttribute(MemberInfo member)
 {
     return(DTOMemberAttribute.GetAttribute(member));
 }
        private static MemberSerializationInfo CreateByCollection(Type targetType, MemberInfo memberInfo, DTOMemberAttribute memberAttribute)
        {
            Type _collectionInterfaceType = null;
            Type _dictionaryInterfaceType = null;

            if (targetType.FindCollectionInterface(ref _collectionInterfaceType,
                                                   ref _dictionaryInterfaceType))
            {
                if (_dictionaryInterfaceType != null) //IDictionary{Key,Value}
                {
                    throw new DTOException("暂时不支持键值对的dto序列化操作");
                }
                //return memberInfo == null ? new DictionarySerializationInfo(targetType) : new DictionarySerializationInfo(memberInfo, memberAttribute);
                else //ICollection{T}
                {
                    return(memberInfo == null ? new CollectionSerializationInfo(targetType) : new CollectionSerializationInfo(memberInfo, memberAttribute));
                }
            }
            return(null);
        }
 public MemberSerializationInfo(MemberInfo memberInfo, DTOMemberAttribute memberAttribute)
 {
     _memberInfo      = memberInfo;
     _memberAttribute = memberAttribute;
 }