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); }
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)Pair.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)); }
public static Property Create(Type valueType, object name, object defaultValue) { if (valueType == typeof(bool)) { return(new BooleanProperty(name, defaultValue ?? false)); } if (valueType == typeof(double)) { return(new DoubleProperty(name, defaultValue ?? 0.0)); } if (valueType == typeof(Tuple <double, double, double>)) { return(new DoubleVector3Property(name, defaultValue ?? Tuple.Create <double, double, double>(0.0, 0.0, 0.0))); } if (valueType == typeof(Pair <double, double>)) { return(new DoubleVectorProperty(name, defaultValue ?? Pair.Create <double, double>(0.0, 0.0))); } if (valueType == typeof(int)) { return(new Int32Property(name, defaultValue ?? 0)); } if (valueType == typeof(string)) { return(new StringProperty(name, (string)defaultValue)); } if (typeof(ImageResource).IsAssignableFrom(valueType)) { return(new ImageProperty(name, (ImageResource)defaultValue)); } if (!valueType.IsEnum) { return(new ObjectProperty(name, defaultValue)); } return(StaticListChoiceProperty.CreateForEnum(valueType, name, defaultValue ?? ((object[])Enum.GetValues(valueType))[0], false)); }
protected StaticListChoiceProperty(StaticListChoiceProperty cloneMe, StaticListChoiceProperty sentinelNotUsed) : base(cloneMe, sentinelNotUsed) { this.valueChoices = (object[])cloneMe.valueChoices.Clone(); }