Ejemplo n.º 1
0
        /// <summary>
        /// Returns if the given property should be serialized.
        /// </summary>
        /// <param name="annotationFreeValue">Should a property without any annotations be serialized?</param>
        private bool CanSerializeProperty(PropertyInfo property, MemberInfo[] members, bool annotationFreeValue)
        {
            // We don't serialize delegates
            if (typeof(Delegate).IsAssignableFrom(property.PropertyType))
            {
                return(false);
            }

            // We don't serialize indexers (like : this[int i])
            ParameterInfo[] v_indexParameters = property.GetIndexParameters();
            if (v_indexParameters != null && v_indexParameters.Length > 0)
            {
                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);
            }

            // 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 (_configUsed.SerializeAttributes.Any(t => PortableReflection.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);
            }

            // If it's an auto-property and it has either a public get or a public set method,
            // then we serialize it
            if (IsAutoProperty(property, members) &&
                (publicGetMethod != null || publicSetMethod != null))
            {
                return(true);
            }


            // Otherwise, we don't bother with serialization
            //Only Public Ones!
            return(annotationFreeValue && publicGetMethod != null && publicSetMethod != null);
        }
Ejemplo n.º 2
0
        private bool CanSerializeField(FieldInfo field, bool annotationFreeValue)
        {
            // We don't serialize delegates
            if (typeof(Delegate).IsAssignableFrom(field.FieldType))
            {
                return(false);
            }

            // We don't serialize compiler generated fields.
            if (field.IsDefined(typeof(CompilerGeneratedAttribute), false))
            {
                return(false);
            }

            // We don't serialize static fields
            if (field.IsStatic)
            {
                return(false);
            }

            // We want to serialize any fields annotated with one of the serialize attributes.
            //
            // NOTE: This occurs *after* the static check, because we *never* want to serialize
            //       static fields.
            if (_configUsed.SerializeAttributes.Any(t => PortableReflection.HasAttribute(field, t)))
            {
                return(true);
            }

            // We use !IsPublic because that also checks for internal, protected, and private.
            //if (!annotationFreeValue && !field.IsPublic) {
            //    return false;
            //}

            //OptOut is only available to Public Members, like in Newtonsoft
            if (annotationFreeValue && field.IsPublic)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        private void CollectProperties(List <MetaProperty> properties, Type reflectedType)
        {
            // do we require a [SerializeField] or [Property] attribute?
            bool requireOptIn  = _configUsed.MemberSerialization == MemberSerialization.OptIn;
            bool requireOptOut = _configUsed.MemberSerialization == MemberSerialization.OptOut;

            ObjectAttribute attr = PortableReflection.GetAttribute <ObjectAttribute>(reflectedType);

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

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

                /*else
                 * {
                 *  //We must check if CanIgnoreMethodInfo is Defined in Ignore Attribute
                 *  var memberIgnoreAttr = PortableReflection.GetAttribute<IgnoreAttribute>(member);
                 *  if (memberIgnoreAttr != null)
                 *  {
                 *      canIgnoreMethodInfo = memberIgnoreAttr.GetCanIgnoreMethodInfo(reflectedType);
                 *      if (canIgnoreMethodInfo == null)
                 *          return true;
                 *  }
                 * }*/

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

                // If an opt-in annotation is required, then skip the property if it doesn't have one
                // of the serialize attributes
                if (requireOptIn &&
                    !_configUsed.SerializeAttributes.Any(t => PortableReflection.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 &&
                 *  _configUsed.IgnoreSerializeAttributes.Any(t => PortableReflection.HasAttribute(member, t))) {
                 *
                 *  continue;
                 * }*/

                if (property != null)
                {
                    if (CanSerializeProperty(property, members, requireOptOut))
                    {
                        var serializePropertyAttr = PortableReflection.GetAttribute <SerializePropertyAttribute>(property);
                        properties.Add(new MetaProperty(property,
                                                        serializePropertyAttr != null ?
                                                        serializePropertyAttr.GetCanSerializeInContextMethodInfo(reflectedType) :
                                                        null));
                    }
                }
                else if (field != null)
                {
                    //We can serialize public field if is default
                    if (CanSerializeField(field, requireOptOut || (!requireOptIn && !requireOptOut)))
                    {
                        var serializeFieldAttr = PortableReflection.GetAttribute <SerializePropertyAttribute>(field);
                        properties.Add(new MetaProperty(field,
                                                        serializeFieldAttr != null ?
                                                        serializeFieldAttr.GetCanSerializeInContextMethodInfo(reflectedType) :
                                                        null));
                    }
                }
            }

            if (reflectedType.Resolve().BaseType != null)
            {
                CollectProperties(properties, reflectedType.Resolve().BaseType);
            }
        }