Browse Source

Don't try to set direct properties to UnsetValue.

Steven Kirk 10 years ago
parent
commit
212426ee1a
2 changed files with 34 additions and 1 deletions
  1. 15 1
      src/Perspex.Base/PerspexObject.cs
  2. 19 0
      src/Perspex.Base/Utilities/TypeUtilities.cs

+ 15 - 1
src/Perspex.Base/PerspexObject.cs

@@ -501,6 +501,7 @@ namespace Perspex
         {
             Contract.Requires<ArgumentNullException>(property != null);
             VerifyAccess();
+
             if (property.IsDirect)
             {
                 property = GetRegistered(property);
@@ -511,7 +512,7 @@ namespace Perspex
                 }
 
                 LogPropertySet(property, value, priority);
-                property.Setter(this, value);
+                property.Setter(this, UnsetToDefault(value, property));
             }
             else
             {
@@ -848,6 +849,19 @@ namespace Perspex
             }
         }
 
+        /// <summary>
+        /// Converts an unset value to the default value for a property type.
+        /// </summary>
+        /// <param name="value">The value.</param>
+        /// <param name="property">The property.</param>
+        /// <returns>The value.</returns>
+        private static object UnsetToDefault(object value, PerspexProperty property)
+        {
+            return value == PerspexProperty.UnsetValue ?
+                TypeUtilities.Default(property.PropertyType) :
+                value;
+        }
+
         /// <summary>
         /// Creates a <see cref="PriorityValue"/> for a <see cref="PerspexProperty"/>.
         /// </summary>

+ 19 - 0
src/Perspex.Base/Utilities/TypeUtilities.cs

@@ -127,5 +127,24 @@ namespace Perspex.Utilities
                 return null;
             }
         }
+
+        /// <summary>
+        /// Gets the default value for the specified type.
+        /// </summary>
+        /// <param name="type">The type.</param>
+        /// <returns>The default value.</returns>
+        public static object Default(Type type)
+        {
+            var typeInfo = type.GetTypeInfo();
+
+            if (typeInfo.IsValueType)
+            {
+                return Activator.CreateInstance(type);
+            }
+            else
+            {
+                return null;
+            }
+        }
     }
 }