Exemplo n.º 1
0
        public static StaticListChoiceProperty CreateForEnum(Type enumType, object name, object defaultValue, bool readOnly)
        {
            // [Flags] enums aren't currently supported
            object[] flagsAttributes = enumType.GetCustomAttributes(typeof(FlagsAttribute), true);

            if (flagsAttributes.Length > 0)
            {
                throw new ArgumentOutOfRangeException("Enums with [Flags] are not currently supported");
            }

            Array enumChoices        = Enum.GetValues(enumType);
            int   defaultChoiceIndex = Array.IndexOf(enumChoices, defaultValue);

            if (defaultChoiceIndex == -1)
            {
                throw new ArgumentOutOfRangeException(string.Format(
                                                          "defaultValue ({0}) is not a valid enum value for {1}",
                                                          defaultValue.ToString(),
                                                          enumType.FullName));
            }

            object[] enumChoicesObj = new object[enumChoices.Length];
            enumChoices.CopyTo(enumChoicesObj, 0);

            StaticListChoiceProperty enumProperty = new StaticListChoiceProperty(name, enumChoicesObj, defaultChoiceIndex, readOnly);

            return(enumProperty);
        }
Exemplo n.º 2
0
        public static Property Create(Type valueType, object name, object defaultValue)
        {
            // TODO: find some way to do this better, using attributes+reflection or something, yes?

            if (valueType == typeof(bool))
            {
                return(new BooleanProperty(name, (bool)(defaultValue ?? (object)false)));
            }
            else if (valueType == typeof(double))
            {
                return(new DoubleProperty(name, (double)(defaultValue ?? (object)0.0)));
            }
            else if (valueType == typeof(Pair <double, double>))
            {
                return(new DoubleVectorProperty(name, (Pair <double, double>)(defaultValue ?? (object)PairUtils.Create(0.0, 0.0))));
            }
            else if (valueType == typeof(int))
            {
                return(new Int32Property(name, (int)(defaultValue ?? (object)0)));
            }
            else if (valueType == typeof(string))
            {
                return(new StringProperty(name, (string)defaultValue));
            }
            else if (typeof(ImageResource).IsAssignableFrom(valueType))
            {
                return(new ImageProperty(name, (ImageResource)defaultValue));
            }
            else if (valueType.IsEnum)
            {
                return(StaticListChoiceProperty.CreateForEnum(
                           valueType,
                           name,
                           defaultValue ?? ((object[])Enum.GetValues(valueType))[0],
                           false));
            }

            throw new ArgumentException(string.Format("Not a valid type: {0}", valueType.FullName));
        }
Exemplo n.º 3
0
 protected StaticListChoiceProperty(StaticListChoiceProperty cloneMe, StaticListChoiceProperty sentinelNotUsed)
     : base(cloneMe, sentinelNotUsed)
 {
     this.valueChoices = (object[])cloneMe.valueChoices.Clone();
 }