예제 #1
0
        private ValueDrawerTypeInfo(Type type)
            : base(type)
        {
            Ensure.Argument.NotNull(type, "type");
            Ensure.Argument.Is(type, "type", ValueDrawerTypes.Root);

            if (Type.IsGenericType)
            {
                GenericDefinitionInfo = Type.IsGenericTypeDefinition ? this : Get(Type.GetGenericTypeDefinition());
            }

            var argsForConstraints = GenericDefinitionInfo != null
                ? GenericDefinitionInfo.GenericArguments : GenericArguments;

            if (argsForConstraints.Owner.IsGenericParameter)
            {
                ConstraintsOnOwner = new GenericParameterConstraints(argsForConstraints.Owner);
            }
            if (argsForConstraints.Value.IsGenericParameter)
            {
                ConstraintsOnValue = new GenericParameterConstraints(argsForConstraints.Value);
            }

            if (ConstraintsOnValue != null)
            {
                IsInterfaceDrawer = ConstraintsOnValue.TypeCount > 0 &&
                                    ConstraintsOnValue.Types.All(t => t.IsInterface);
            }
        }
예제 #2
0
        private static void InitValueToDrawerMapping()
        {
            AddValueToDrawerMapping(typeof(Enum), typeof(EnumDrawer <,>));

            foreach (var def in Definitions)
            {
                var typeInfo = ValueDrawerTypeInfo.Get(def);

                var value = typeInfo.GenericArguments.Value;
                if (!value.IsGenericParameter && (value.IsGenericType || !value.ContainsGenericParameters))
                {
                    AddValueToDrawerMapping(value, def);
                }

                var valueConstraints = typeInfo.ConstraintsOnValue;
                if (valueConstraints != null && valueConstraints.TypeCount == 1)
                {
                    var constraintType = valueConstraints.GetType(0);
                    if (constraintType != typeof(ValueType) && constraintType != typeof(Enum))
                    {
                        AddValueToDrawerMapping(constraintType, def);
                    }
                }
            }

            //UnityEngine.Debug.Log(StringUtil.Join(valueToDrawer, p => string.Format("{0}, {1}", p.Key, p.Value), "\n"));
        }
예제 #3
0
        private int CompareImpl(Type l, Type r)
        {
            int value;

            if (!CompareUtil.PreCompare(l, r, out value))
            {
                return(value);
            }

            // Use default compare if any argument is not type of ValueDrawer.
            var root = ValueDrawerTypes.Root;

            if (!root.IsAssignableFrom(l) || !root.IsAssignableFrom(r))
            {
                return(Comparer <Type> .Default.Compare(l, r));
            }

            var priorities = ValueDrawerPrioritiesForType.Get(ValueType ?? typeof(object));

            if (priorities != null)
            {
                var lg = l.IsGenericType ? l.GetGenericTypeDefinition() : l;
                var rg = r.IsGenericType ? r.GetGenericTypeDefinition() : r;
                int lp = priorities.GetPriority(lg), rp = priorities.GetPriority(rg);
                return(lp.CompareTo(rp));
            }

            ValueDrawerTypeInfo lInfo = ValueDrawerTypeInfo.Get(l), rInfo = ValueDrawerTypeInfo.Get(r);

            if (lInfo != null && rInfo != null)
            {
                // Interface drawer is more appropriate for interface type.
                if (lInfo.IsInterfaceDrawer && !rInfo.IsInterfaceDrawer)
                {
                    return(ValueType.IsInterface ? 1 : -1);
                }
                if (!lInfo.IsInterfaceDrawer && rInfo.IsInterfaceDrawer)
                {
                    return(ValueType.IsInterface ? -1 : 1);
                }

                GenericParameterConstraints lcov = lInfo.ConstraintsOnValue, rcov = rInfo.ConstraintsOnValue;
                if (lcov != null && rcov != null)
                {
                    if (lcov.TypeCount == 1 && rcov.TypeCount == 1)
                    {
                        Type lIf = rcov.GetType(0), rIf = rcov.GetType(0);
                        if (lIf != rIf)
                        {
                            if (lIf.IsAssignableFrom(rIf))
                            {
                                return(-1);
                            }
                            if (rIf.IsAssignableFrom(lIf))
                            {
                                return(1);
                            }
                        }
                    }
                }

                // The more the drawer type derived, the higher priority it owned.
                return(lInfo.DeriveDepth.CompareTo(rInfo.DeriveDepth));
            }

            return(0);
        }
예제 #4
0
 protected ValueDrawer()
 {
     TypeInfo        = ValueDrawerTypeInfo.Get(GetType());
     RootValueDrawer = this;
     Name            = TypeInfo.GenericArguments.Value.FullName;
 }