コード例 #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;

            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);
        }
コード例 #2
0
 protected override DTOMemberAttribute GetMemberAttribute(MemberInfo member)
 {
     return(DTOMemberAttribute.GetAttribute(member));
 }