Esempio n. 1
0
        /// <summary>
        /// fetches all the relevant AbstractTypeInspectors for target including fields, properties and methods.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static List <AbstractTypeInspector> getInspectableProperties(object target)
        {
            var props      = new List <AbstractTypeInspector>();
            var targetType = target.GetType();

            var fields = ReflectionUtils.getFields(targetType);

            foreach (var field in fields)
            {
                if (!field.IsPublic && IEnumerableExt.count(field.GetCustomAttributes <InspectableAttribute>()) == 0)
                {
                    continue;
                }

                if (field.IsInitOnly)
                {
                    continue;
                }

                // skip enabled which is handled elsewhere
                if (field.Name == "enabled" || field.Name == "entity")
                {
                    continue;
                }

                var inspector = getInspectorForType(field.FieldType, target, field);
                if (inspector != null)
                {
                    inspector.setTarget(target, field);
                    inspector.initialize();
                    props.Add(inspector);
                }
            }

            var properties = ReflectionUtils.getProperties(targetType);

            foreach (var prop in properties)
            {
                if (!prop.CanRead || !prop.CanWrite)
                {
                    continue;
                }

                if ((!prop.GetMethod.IsPublic || !prop.SetMethod.IsPublic) && IEnumerableExt.count(prop.GetCustomAttributes <InspectableAttribute>()) == 0)
                {
                    continue;
                }

                // skip Component.enabled which is handled elsewhere
                if (prop.Name == "enabled" || prop.Name == "entity")
                {
                    continue;
                }

                var inspector = getInspectorForType(prop.PropertyType, target, prop);
                if (inspector != null)
                {
                    inspector.setTarget(target, prop);
                    inspector.initialize();
                    props.Add(inspector);
                }
            }

            var methods = ReflectionUtils.getMethods(targetType);

            foreach (var method in methods)
            {
                var attr = method.GetCustomAttribute <InspectorCallableAttribute>();
                if (attr == null)
                {
                    continue;
                }

                if (!MethodInspector.areParametersValid(method.GetParameters()))
                {
                    continue;
                }

                var inspector = new MethodInspector();
                inspector.setTarget(target, method);
                inspector.initialize();
                props.Add(inspector);
            }

            return(props);
        }
        /// <summary>
        /// fetches all the relevant AbstractTypeInspectors for target including fields, properties and methods.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static List <AbstractTypeInspector> GetInspectableProperties(object target)
        {
            List <AbstractTypeInspector> inspectors = new List <AbstractTypeInspector>();
            Type targetType          = target.GetType();
            bool isComponentSubclass = target is Component;

            IEnumerable <FieldInfo> fields = ReflectionUtils.GetFields(targetType);

            foreach (FieldInfo field in fields)
            {
                if (field.IsStatic || field.IsDefined(notInspectableAttrType))
                {
                    continue;
                }

                bool hasInspectableAttribute = field.IsDefined(inspectableAttrType);

                // private fields must have the InspectableAttribute
                if (!field.IsPublic && !hasInspectableAttribute)
                {
                    continue;
                }

                // similarly, readonly fields must have the InspectableAttribute
                if (field.IsInitOnly && !hasInspectableAttribute)
                {
                    continue;
                }

                // skip enabled and entity which is handled elsewhere if this is a Component
                if (isComponentSubclass && (field.Name == "Enabled" || field.Name == "Entity"))
                {
                    continue;
                }

                AbstractTypeInspector inspector = GetInspectorForType(field.FieldType, target, field);
                if (inspector != null)
                {
                    inspector.SetTarget(target, field);
                    inspector.Initialize();
                    inspectors.Add(inspector);
                }
            }

            IEnumerable <PropertyInfo> properties = ReflectionUtils.GetProperties(targetType);

            foreach (PropertyInfo prop in properties)
            {
                if (prop.IsDefined(notInspectableAttrType))
                {
                    continue;
                }

                // Transforms and Component subclasses arent useful to inspect
                if (prop.PropertyType == transformType || prop.PropertyType.IsSubclassOf(componentType))
                {
                    continue;
                }

                if (!prop.CanRead || prop.GetGetMethod(true).IsStatic)
                {
                    continue;
                }

                bool hasInspectableAttribute = prop.IsDefined(inspectableAttrType);

                // private props must have the InspectableAttribute
                if (!prop.GetMethod.IsPublic && !hasInspectableAttribute)
                {
                    continue;
                }

                // similarly, readonly props must have the InspectableAttribute
                if (!prop.CanWrite && !hasInspectableAttribute)
                {
                    continue;
                }

                // skip Component.enabled  and entity which is handled elsewhere
                if (isComponentSubclass && (prop.Name == "Enabled" || prop.Name == "Entity"))
                {
                    continue;
                }

                AbstractTypeInspector inspector = GetInspectorForType(prop.PropertyType, target, prop);
                if (inspector != null)
                {
                    inspector.SetTarget(target, prop);
                    inspector.Initialize();
                    inspectors.Add(inspector);
                }
            }

            IEnumerable <MethodInfo> methods = GetAllMethodsWithAttribute <InspectorCallableAttribute>(targetType);

            foreach (MethodInfo method in methods)
            {
                if (!MethodInspector.AreParametersValid(method.GetParameters()))
                {
                    continue;
                }

                MethodInspector inspector = new MethodInspector();
                inspector.SetTarget(target, method);
                inspector.Initialize();
                inspectors.Add(inspector);
            }

            return(inspectors);
        }