From 6ed1f624854d00e44b859d4f18466a32840b24ce Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 9 Jun 2015 23:17:45 +0200 Subject: [PATCH] Added a generic OverrideDefaultValue. --- Perspex.Base/PerspexProperty.cs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Perspex.Base/PerspexProperty.cs b/Perspex.Base/PerspexProperty.cs index 17850e8631..e6184ca510 100644 --- a/Perspex.Base/PerspexProperty.cs +++ b/Perspex.Base/PerspexProperty.cs @@ -346,21 +346,21 @@ namespace Perspex /// True if the value is valid, otherwise false. public bool IsValidValue(object value) { - if (value == UnsetValue) - { - return true; - } - else if (value == null) - { - return !this.PropertyType.GetTypeInfo().IsValueType || - Nullable.GetUnderlyingType(this.PropertyType) != null; - } + return PriorityValue.IsValidValue(value, this.PropertyType); + } - return this.PropertyType.GetTypeInfo().IsAssignableFrom(value.GetType().GetTypeInfo()); + /// + /// Overrides the default value for the property on the specified type. + /// + /// The type. + /// The default value. + public void OverrideDefaultValue(object defaultValue) + { + this.OverrideDefaultValue(typeof(T), defaultValue); } /// - /// Gets the default value for the property on the specified type. + /// Overrides the default value for the property on the specified type. /// /// The type. /// The default value. @@ -368,7 +368,15 @@ namespace Perspex { Contract.Requires(type != null); - // TODO: Ensure correct type. + if (!this.IsValidValue(defaultValue)) + { + throw new InvalidOperationException(string.Format( + "Invalid value for Property '{0}': {1} ({2})", + this.Name, + defaultValue, + defaultValue.GetType().FullName)); + } + if (this.defaultValues.ContainsKey(type)) { throw new InvalidOperationException("Default value is already set for this property.");