Exemple #1
0
        /// <summary>
        /// Gets the serialization information about the type members.
        /// This information is used for creation of the corresponding schema nodes.
        /// </summary>
        /// <param name="type">Type containing members which should be serialized.</param>
        /// <returns>
        /// Serialization information about the fields/properties.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">Thrown, if the type argument is null.</exception>
        public override MemberSerializationInfo[] ResolveMembers(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var fields = type
                         .GetAllFields()
                         .Where(f => (f.Attributes & FieldAttributes.Public) != 0 &&
                                (f.Attributes & FieldAttributes.Static) == 0);

            var properties =
                type.GetAllProperties()
                .Where(p =>
                       p.DeclaringType.IsAnonymous() ||
                       p.DeclaringType.IsKeyValuePair() ||
                       (p.CanRead && p.CanWrite && p.GetSetMethod() != null && p.GetGetMethod() != null));

            var serializedProperties = TypeExtensions.RemoveDuplicates(properties);

            return(fields
                   .Concat <MemberInfo>(serializedProperties)
                   .Select(m => new MemberSerializationInfo {
                Name = m.Name, MemberInfo = m, Nullable = m.GetCustomAttributes(false).OfType <NullableSchemaAttribute>().Any()
            })
                   .ToArray());
        }
Exemple #2
0
        /// <summary>
        /// Gets the serialization information about the type members.
        /// This information is used for creation of the corresponding schema nodes.
        /// </summary>
        /// <param name="type">The type, members of which should be serialized.</param>
        /// <returns>
        /// Serialization information about the fields/properties.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">The type argument is null.</exception>
        public override MemberSerializationInfo[] ResolveMembers(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type.IsKeyValuePair())
            {
                var keyValueProperties = type.GetAllProperties();
                return(keyValueProperties
                       .Select(p => new MemberSerializationInfo
                {
                    Name = p.Name,
                    MemberInfo = p,
                    Nullable = false
                })
                       .ToArray());
            }

            var fields     = type.GetAllFields();
            var properties = type.GetAllProperties();

            var dataMemberProperties = properties
                                       .Where(p => p.GetDataMemberAttribute() != null);

            var serializedProperties = TypeExtensions.RemoveDuplicates(dataMemberProperties);

            TypeExtensions.CheckPropertyGetters(serializedProperties);

            var members = fields
                          .Concat <MemberInfo>(serializedProperties)
                          .Select(m => new
            {
                Member    = m,
                Attribute = m.GetCustomAttributes(false).OfType <DataMemberAttribute>().SingleOrDefault(),
                Nullable  = m.GetCustomAttributes(false).OfType <NullableSchemaAttribute>().Any()
            });

            var result = members.Where(m => m.Attribute != null)
                         .Select(m => new MemberSerializationInfo
            {
                Name       = m.Attribute.Name ?? m.Member.Name,
                MemberInfo = m.Member,
                Nullable   = m.Nullable
            });

            if (this.useAlphabeticalOrder)
            {
                result = result.OrderBy(p => p.Name);
            }

            return(result.ToArray());
        }