Пример #1
0
        private List <DataMemberDescription> FindMembers(Type objectType)
        {
            if (objectType == null)
            {
                throw new ArgumentNullException("objectType");
            }

            var members     = new List <DataMemberDescription>();
            var memberNames = new HashSet <string>(StringComparer.Ordinal);

            var isOptIn     = objectType.GetCustomAttributes(false).Any(a => a.GetType().Name == DATA_CONTRACT_ATTRIBUTE_NAME);
            var searchFlags = BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | (isOptIn ? BindingFlags.NonPublic : 0);
            var properties  = objectType.GetProperties(searchFlags);
            var fields      = objectType.GetFields(searchFlags);

            foreach (var member in properties.Cast <MemberInfo>().Concat(fields.Cast <MemberInfo>()))
            {
                if (member is PropertyInfo && (member as PropertyInfo).GetIndexParameters().Length != 0)
                {
                    continue;
                }

                var dataMemberAttribute   = member.GetCustomAttributes(false).FirstOrDefault(a => a.GetType().Name == DATA_MEMBER_ATTRIBUTE_NAME);
                var ignoreMemberAttribute = member.GetCustomAttributes(false).FirstOrDefault(a => a.GetType().Name == IGNORE_DATA_MEMBER_ATTRIBUTE_NAME);

                if (isOptIn && dataMemberAttribute == null)
                {
                    continue;
                }
                else if (!isOptIn && ignoreMemberAttribute != null)
                {
                    continue;
                }

                var dataMember = default(DataMemberDescription);
                if (member is PropertyInfo)
                {
                    dataMember = new PropertyDescription(this, member as PropertyInfo);
                }
                else if (member is FieldInfo)
                {
                    dataMember = new FieldDescription(this, member as FieldInfo);
                }
                else
                {
                    throw new InvalidOperationException("Unknown member type. Should be PropertyInfo or FieldInfo.");
                }

                if (string.IsNullOrEmpty(dataMember.Name))
                {
                    throw JsonSerializationException.TypeIsNotValid(objectType, "has no members with empty name");
                }

                if (memberNames.Contains(dataMember.Name))
                {
                    var conflictingMember = members.First(m => m.Name == dataMember.Name);
                    throw JsonSerializationException.TypeIsNotValid(objectType, string.Format("has no duplicate member's name '{0}' ('{1}.{2}' and '{3}.{4}')", dataMember.Name, conflictingMember.Member.DeclaringType.Name, conflictingMember.Member.Name, dataMember.Member.DeclaringType.Name, dataMember.Member.Name));
                }

                members.Add(dataMember);
                memberNames.Add(dataMember.Name);
            }

            return(members);
        }