Ejemplo n.º 1
0
        private static IEnumerable <MemberInfo> GetSerializableMemberInfos(Type type, IJsonFormatterResolver fallbackResolver)
        {
            var bindingFlags = BindingFlags.Instance | BindingFlags.Public;

            if (fallbackResolver.AllowsPrivate())
            {
                bindingFlags |= BindingFlags.NonPublic;
            }

            // properties
            var allProperties = type
                                .GetProperties(bindingFlags)
                                .ToArray();

            var properties = allProperties
                             .Where(m => m.GetCustomAttribute <IgnoreDataMemberAttribute>() == null)
                             .ToArray();

            // fields
            var explicitlyIgnoredProperties = allProperties
                                              .Where(m => m.GetCustomAttribute <IgnoreDataMemberAttribute>() != null)
                                              .Select(p => p.Name);

            var propInfoNames = new HashSet <string>(properties.Select(p => p.Name).Concat(explicitlyIgnoredProperties));

            var fields = type
                         .GetFields(bindingFlags)
                         .Where(m => m.GetCustomAttribute <IgnoreDataMemberAttribute>() == null)
                         .Where(f => !IsBackingField(f) || !IsBackingFieldAlreadyIncluded(f, propInfoNames));

            return(properties
                   .Cast <MemberInfo>()
                   .Concat(fields));
        }