Ejemplo n.º 1
0
        /// <summary>
        /// Gets the list of members available on a type as popup options.
        /// </summary>
        protected virtual List <DropdownOption <UnityMemberBase> > GetTypeMemberOptions(Type type, string component = null)
        {
            var options = new List <DropdownOption <UnityMemberBase> >();

            options = type
                      .GetMembers(validBindingFlags)
                      .Where(member => EnumEx.HasFlag(validMemberTypes, member.MemberType))
                      .Where(ValidateMember)
                      .Select(member => GetMemberOption(member, component, member.DeclaringType != type))
                      .ToList();


            //Debug.Log ("Type: " + type.FullName + "Label:" + options [0].label);

            if (filter.Extension)
            {
                var extensionMethods = type.GetExtensionMethods(filter.Inherited)
                                       .Where(ValidateMember)
                                       .Select(method => GetMemberOption(method, component, filter.Inherited && method.GetParameters()[0].ParameterType != type));

                options.AddRange(extensionMethods);
            }

            // Sort the options

            options.Sort((a, b) =>
            {
                var aSub = a.label.Contains("/");
                var bSub = b.label.Contains("/");

                if (aSub != bSub)
                {
                    return(bSub.CompareTo(aSub));
                }
                else
                {
                    return(a.value.name.CompareTo(b.value.name));
                }
            });

            return(options);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determines whether a MemberInfo of the given type should be included in the options.
        /// </summary>
        protected virtual bool ValidateMemberType(Type type)
        {
            bool validFamily = false;
            bool validType;

            // Allow type families based on the filter attribute

            TypeFamily families = filter.TypeFamilies;

            if (EnumEx.HasFlag(families, TypeFamily.Array))
            {
                validFamily |= type.IsArray;
            }
            if (EnumEx.HasFlag(families, TypeFamily.Class))
            {
                validFamily |= type.IsClass;
            }
            if (EnumEx.HasFlag(families, TypeFamily.Enum))
            {
                validFamily |= type.IsEnum;
            }
            if (EnumEx.HasFlag(families, TypeFamily.Interface))
            {
                validFamily |= type.IsInterface;
            }
            if (EnumEx.HasFlag(families, TypeFamily.Primitive))
            {
                validFamily |= type.IsPrimitive;
            }
            if (EnumEx.HasFlag(families, TypeFamily.Reference))
            {
                validFamily |= !type.IsValueType;
            }
            if (EnumEx.HasFlag(families, TypeFamily.Value))
            {
                validFamily |= (type.IsValueType && type != typeof(void));
            }
            if (EnumEx.HasFlag(families, TypeFamily.Void))
            {
                validFamily |= type == typeof(void);
            }

            // Allow types based on the filter attribute
            // If no filter types are specified, all types are allowed.

            if (filter.Types.Count > 0)
            {
                validType = false;

                foreach (Type allowedType in filter.Types)
                {
                    if (allowedType.IsAssignableFrom(type))
                    {
                        validType = true;
                        break;
                    }
                }
            }
            else
            {
                validType = true;
            }

            return(validFamily && validType);
        }