Example #1
0
        /// <summary>
        /// Gets the items for the type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="useProperties">True if use type properties.</param>
        /// <param name="useFields">True if use type fields.</param>
        /// <returns>The items.</returns>
        protected List <ItemInfo> GetItemsForType(ScriptType type, bool useProperties, bool useFields)
        {
            var items = new List <ItemInfo>();

            if (useProperties)
            {
                // Process properties
                var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                items.Capacity = Math.Max(items.Capacity, items.Count + properties.Length);
                for (int i = 0; i < properties.Length; i++)
                {
                    var p            = properties[i];
                    var attributes   = p.GetAttributes(true);
                    var showInEditor = attributes.Any(x => x is ShowInEditorAttribute);

                    // Skip properties without getter or setter
                    if (!p.HasGet || (!p.HasSet && !showInEditor))
                    {
                        continue;
                    }

                    // Skip hidden fields, handle special attributes
                    if ((!p.IsPublic && !showInEditor) || attributes.Any(x => x is HideInEditorAttribute))
                    {
                        continue;
                    }

                    items.Add(new ItemInfo(p, attributes));
                }
            }

            if (useFields)
            {
                // Process fields
                var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                items.Capacity = Math.Max(items.Capacity, items.Count + fields.Length);
                for (int i = 0; i < fields.Length; i++)
                {
                    var f = fields[i];

                    var attributes = f.GetAttributes(true);

                    // Skip hidden fields, handle special attributes
                    if ((!f.IsPublic && !attributes.Any(x => x is ShowInEditorAttribute)) || attributes.Any(x => x is HideInEditorAttribute))
                    {
                        continue;
                    }

                    items.Add(new ItemInfo(f, attributes));
                }
            }

            return(items);
        }
Example #2
0
        private static void GetEntries(object instance, Stack <MemberInfoPath.Entry> membersPath, ScriptType type, List <TypeEntry> result, List <object> values, Stack <object> refStack)
        {
            // Note: this should match Flax serialization rules and attributes (see ExtendedDefaultContractResolver)

            var fields     = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            for (int i = 0; i < fields.Length; i++)
            {
                var f          = fields[i];
                var attributes = f.GetAttributes();

                // Serialize non-public fields only with a proper attribute
                if (!f.IsPublic && !attributes.Any(x => x is SerializeAttribute))
                {
                    continue;
                }

                // Check if has attribute to skip serialization
                bool noSerialize = false;
                foreach (var attribute in attributes)
                {
                    if (AttributesIgnoreList.Contains(attribute.GetType()))
                    {
                        noSerialize = true;
                        break;
                    }
                }
                if (noSerialize)
                {
                    continue;
                }

                var memberValue = f.GetValue(instance);
                GetEntries(new MemberInfoPath.Entry(f), membersPath, result, values, refStack, GetMemberType(f.ValueType, memberValue), memberValue);
            }

            for (int i = 0; i < properties.Length; i++)
            {
                var p = properties[i];

                // Serialize only properties with read/write
                if (!p.HasGet || !p.HasSet)
                {
                    continue;
                }

                var attributes = p.GetAttributes();

                // Serialize non-public properties only with a proper attribute
                if (!p.IsPublic && !attributes.Any(x => x is SerializeAttribute))
                {
                    continue;
                }

                // Check if has attribute to skip serialization
                bool noSerialize = false;
                foreach (var attribute in attributes)
                {
                    if (AttributesIgnoreList.Contains(attribute.GetType()))
                    {
                        noSerialize = true;
                        break;
                    }
                }
                if (noSerialize)
                {
                    continue;
                }

                var memberValue = p.GetValue(instance);
                GetEntries(new MemberInfoPath.Entry(p), membersPath, result, values, refStack, GetMemberType(p.ValueType, memberValue), memberValue);
            }
        }