コード例 #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;

            // 检查是否有[fsObject] attribute
            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
                // 跳过attribute 是 typeof(NonSerializedAttribute) 或者 typeof(fsIgnoreAttribute)
                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;
                }

                // 如果设置了requireOptIn,跳过那些Attribute不是UnityEngine.SerializeField和fsPropertyAttribute的
                // 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;
                }

                // 如果设置了requireOptOut,跳过Attribute是 typeof(NonSerializedAttribute), typeof(fsIgnoreAttribute)的
                // 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)
                {
                    // 判断是否可以序列化这个Property
                    if (CanSerializeProperty(config, property, members, requireOptOut))
                    {
                        properties.Add(new fsMetaProperty(config, property));
                    }
                }
                // 如果是字段的话
                else if (field != null)
                {
                    // 判断是否可以序列化这个Field
                    if (CanSerializeField(config, field, requireOptOut))
                    {
                        properties.Add(new fsMetaProperty(config, field));
                    }
                }
            }

            // 处理基类,收集基类的属性
            if (reflectedType.Resolve().BaseType != null)
            {
                CollectProperties(config, properties, reflectedType.Resolve().BaseType);
            }
        }
コード例 #2
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;
                }

                // 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);
            }
        }