Exemplo n.º 1
0
        private static void CollectProperties(fsConfig config, List <fsMetaProperty> properties, Type reflectedType)
        {
            // do we require a [SerializeField] or [fsProperty] attribute?
            bool requireOptIn  = config.DefaultMemberSerialization == fsMemberSerialization.OptIn;
            bool requireOptOut = config.DefaultMemberSerialization == fsMemberSerialization.OptOut;

            fsObjectAttribute attr = fsPortableReflection.GetAttribute <fsObjectAttribute>(reflectedType);

            if (attr != null)
            {
                requireOptIn  = attr.MemberSerialization == fsMemberSerialization.OptIn;
                requireOptOut = attr.MemberSerialization == fsMemberSerialization.OptOut;
            }

            MemberInfo[] members = reflectedType.GetDeclaredMembers();
            foreach (var member in members)
            {
                // We don't serialize members annotated with any of the ignore
                // serialize attributes
                if (config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t)))
                {
                    continue;
                }

                PropertyInfo property = member as PropertyInfo;
                FieldInfo    field    = member as FieldInfo;

                // Early out if it's neither a field or a property, since we
                // don't serialize anything else.
                if (property == null && field == null)
                {
                    continue;
                }

                // Skip properties if we don't want them, to avoid the cost of
                // checking attributes.
                if (property != null && !config.EnablePropertySerialization)
                {
                    continue;
                }

                // If an opt-in annotation is required, then skip the property if
                // it doesn't have one of the serialize attributes
                if (requireOptIn &&
                    !config.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t)))
                {
                    continue;
                }

                // If an opt-out annotation is required, then skip the property
                // *only if* it has one of the not serialize attributes
                if (requireOptOut &&
                    config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t)))
                {
                    continue;
                }

                if (property != null)
                {
                    if (CanSerializeProperty(config, property, members, requireOptOut))
                    {
                        properties.Add(new fsMetaProperty(config, property));
                    }
                }
                else if (field != null)
                {
                    if (CanSerializeField(config, field, requireOptOut))
                    {
                        properties.Add(new fsMetaProperty(config, field));
                    }
                }
            }

            if (reflectedType.Resolve().BaseType != null)
            {
                CollectProperties(config, properties, reflectedType.Resolve().BaseType);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns if the given property should be serialized.
        /// </summary>
        /// <param name="annotationFreeValue">
        /// Should a property without any annotations be serialized?
        /// </param>
        private static bool CanSerializeProperty(fsConfig config, PropertyInfo property, MemberInfo[] members, bool annotationFreeValue)
        {
            // We don't serialize delegates
            if (typeof(Delegate).IsAssignableFrom(property.PropertyType))
            {
                return(false);
            }

            var publicGetMethod = property.GetGetMethod(/*nonPublic:*/ false);
            var publicSetMethod = property.GetSetMethod(/*nonPublic:*/ false);

            // We do not bother to serialize static fields.
            if ((publicGetMethod != null && publicGetMethod.IsStatic) ||
                (publicSetMethod != null && publicSetMethod.IsStatic))
            {
                return(false);
            }

            // Never serialize indexers. I can't think of a sane way to
            // serialize/deserialize them, and they're normally wrappers around
            // other fields anyway...
            if (property.GetIndexParameters().Length > 0)
            {
                return(false);
            }

            // Don't serialize members of types that themselves aren't to be serialized.
            if (config.IgnoreSerializeTypeAttributes.Any(t => fsPortableReflection.HasAttribute(property.PropertyType, t)))
            {
                return(false);
            }

            // If a property is annotated with one of the serializable
            // attributes, then it should it should definitely be serialized.
            //
            // NOTE: We place this override check *after* the static check,
            //       because we *never* allow statics to be serialized.
            if (config.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(property, t)))
            {
                return(true);
            }

            // If the property cannot be both read and written to, we are not
            // going to serialize it regardless of the default serialization mode
            if (property.CanRead == false || property.CanWrite == false)
            {
                return(false);
            }

            // Depending on the configuration options, check whether the property
            // is automatic and if it has a public setter to determine whether it
            // should be serialized
            if ((publicGetMethod != null && (config.SerializeNonPublicSetProperties || publicSetMethod != null)) &&
                (config.SerializeNonAutoProperties || IsAutoProperty(property, members)))
            {
                return(true);
            }

            // Otherwise, we don't bother with serialization
            return(annotationFreeValue);
        }