Don't throw an unnecessary exception if we can recognize the exceptional situation

This commit is contained in:
Michael Becker 2015-04-30 13:55:52 -04:00
parent f566e1aa86
commit 7dd420f392

View File

@ -152,15 +152,12 @@ namespace UniversalEditor.ObjectModels.PropertyList
}
else
{
try
{
result = (T)parent.Properties[propertyPath[propertyPath.Length - 1]].Value;
}
catch (NullReferenceException)
Property p = parent.Properties[propertyPath[propertyPath.Length - 1]];
if (p == null || p.Value == null)
{
result = defaultValue;
}
catch (InvalidCastException)
else if (p.Value.GetType() != typeof(T) && p.Value.GetType() == typeof(String))
{
Type t = typeof(T);
MethodInfo miParse = t.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new Type[]
@ -173,6 +170,30 @@ namespace UniversalEditor.ObjectModels.PropertyList
});
result = (T)retvalobj;
}
else
{
try
{
result = (T)parent.Properties[propertyPath[propertyPath.Length - 1]].Value;
}
catch (NullReferenceException)
{
result = defaultValue;
}
catch (InvalidCastException)
{
Type t = typeof(T);
MethodInfo miParse = t.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new Type[]
{
typeof(string)
}, null);
object retvalobj = miParse.Invoke(null, new object[]
{
parent.Properties[propertyPath[propertyPath.Length - 1]].Value
});
result = (T)retvalobj;
}
}
}
}
}