示例#1
0
        /// <summary>
        ///   Gets all properties of the specified object which are flagged with the ExposeProperty attribute
        ///   and are supported by Unity inspectors.
        /// </summary>
        /// <param name="obj">Object to get the properties of.</param>
        /// <returns>
        ///   All properties of the specified object which are flagged with the ExposeProperty attribute
        ///   and are supported by Unity inspectors.
        /// </returns>
        public static PropertyField[] GetProperties(object obj)
        {
            List <PropertyField> fields = new List <PropertyField>();

            PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo info in infos)
            {
                // Make sure property has a public getter.
                if (!info.CanRead)
                {
                    continue;
                }

                object[] attributes = info.GetCustomAttributes(typeof(ExposePropertyAttribute), true);
                bool     isExposed  = attributes.Length > 0;
                if (!isExposed)
                {
                    continue;
                }

                // Check that property has a supported type.
                SerializedPropertyType type;
                Func <object, object>  getConversionFunc;
                Func <object, object>  setConversionFunc;
                if (!PropertyField.GetPropertyType(info, out type, out getConversionFunc, out setConversionFunc))
                {
                    continue;
                }

                PropertyField field = new PropertyField(obj, info, type)
                {
                    GetConversionFunc = getConversionFunc,
                    SetConversionFunc = setConversionFunc
                };
                fields.Add(field);
            }

            return(fields.ToArray());
        }
        /// <summary>
        ///   Gets all properties of the specified object which are flagged with the ExposeProperty attribute
        ///   and are supported by Unity inspectors.
        /// </summary>
        /// <param name="obj">Object to get the properties of.</param>
        /// <returns>
        ///   All properties of the specified object which are flagged with the ExposeProperty attribute
        ///   and are supported by Unity inspectors.
        /// </returns>
        public static PropertyField[] GetProperties(object obj)
        {
            List<PropertyField> fields = new List<PropertyField>();

            PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo info in infos)
            {
                // Make sure property has a public getter.
                if (!info.CanRead)
                {
                    continue;
                }

                object[] attributes = info.GetCustomAttributes(typeof(ExposePropertyAttribute), true);
                bool isExposed = attributes.Length > 0;
                if (!isExposed)
                {
                    continue;
                }

                // Check that property has a supported type.
                SerializedPropertyType type;
                Func<object, object> getConversionFunc;
                Func<object, object> setConversionFunc;
                if (!PropertyField.GetPropertyType(info, out type, out getConversionFunc, out setConversionFunc))
                {
                    continue;
                }

                PropertyField field = new PropertyField(obj, info, type)
                    {
                        GetConversionFunc = getConversionFunc,
                        SetConversionFunc = setConversionFunc
                    };
                fields.Add(field);
            }

            return fields.ToArray();
        }