Browse Source

Don't try to set direct properties to UnsetValue.

pull/278/head
Steven Kirk 11 years ago
parent
commit
212426ee1a
  1. 16
      src/Perspex.Base/PerspexObject.cs
  2. 19
      src/Perspex.Base/Utilities/TypeUtilities.cs

16
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
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;
}
}
}
}

Loading…
Cancel
Save