PropertyEditorConverters - reusing TryConvertTo<T>, removing duplicate code.

This commit is contained in:
leekelleher
2012-11-10 21:41:40 +00:00
parent a3895ab149
commit ee3e3787a8
2 changed files with 2 additions and 22 deletions

View File

@@ -21,16 +21,7 @@ namespace Umbraco.Core.PropertyEditors
/// <returns></returns>
public Attempt<object> ConvertPropertyValue(object value)
{
if (value == null) return new Attempt<object>(false, null);
//if its already a DateTime
if (TypeHelper.IsTypeAssignableFrom<DateTime>(value))
return new Attempt<object>(true, value);
//convert to string
var asString = Convert.ToString(value);
DateTime dt;
return DateTime.TryParse(asString, out dt)
? new Attempt<object>(true, dt)
: new Attempt<object>(false, null);
return value.TryConvertTo(typeof(DateTime));
}
}
}

View File

@@ -16,18 +16,7 @@ namespace Umbraco.Core.PropertyEditors
/// <returns></returns>
public Attempt<object> ConvertPropertyValue(object value)
{
if (value == null) return new Attempt<object>(false, null);
//if its already a bool
if (TypeHelper.IsTypeAssignableFrom<bool>(value))
return new Attempt<object>(true, value);
//convert to string
var asString = Convert.ToString(value);
bool asBool;
return bool.TryParse(asString, out asBool)
? new Attempt<object>(true, asBool)
: bool.TryParse(asString.Replace("1", "true").Replace("0", "false"), out asBool) //convert 0 or 1 to true or false
? new Attempt<object>(true, asBool)
: new Attempt<object>(false, null);
return value.TryConvertTo(typeof(bool));
}
}
}