docs: Better exception messages when failing to convert type

This commit is contained in:
Chris Cameron
2018-04-09 16:22:48 -04:00
parent 15199b7f4e
commit 0fd8d91850

View File

@@ -195,6 +195,24 @@ namespace ICD.Common.Utils
!= null; != null;
} }
/// <summary>
/// Platform independant delegate instantiation.
/// </summary>
/// <param name="type"></param>
/// <param name="firstArgument"></param>
/// <param name="method"></param>
/// <returns></returns>
public static Delegate CreateDelegate(Type type, object firstArgument, MethodInfo method)
{
return
#if SIMPLSHARP
CDelegate
#else
Delegate
#endif
.CreateDelegate(type, firstArgument, method);
}
/// <summary> /// <summary>
/// Creates an instance of the given type, calling the default constructor. /// Creates an instance of the given type, calling the default constructor.
/// </summary> /// </summary>
@@ -368,13 +386,15 @@ namespace ICD.Common.Utils
if (type.CanBeNull()) if (type.CanBeNull())
return null; return null;
throw new InvalidCastException(); throw new InvalidCastException(string.Format("Unable to convert NULL to type {0}", type.Name));
} }
Type valueType = value.GetType(); Type valueType = value.GetType();
if (valueType.IsAssignableTo(type)) if (valueType.IsAssignableTo(type))
return value; return value;
try
{
// Handle enum // Handle enum
if (type.IsEnum) if (type.IsEnum)
{ {
@@ -387,23 +407,12 @@ namespace ICD.Common.Utils
return Convert.ChangeType(value, type, null); return Convert.ChangeType(value, type, null);
} }
catch (Exception e)
/// <summary>
/// Platform independant delegate instantiation.
/// </summary>
/// <param name="type"></param>
/// <param name="firstArgument"></param>
/// <param name="method"></param>
/// <returns></returns>
public static Delegate CreateDelegate(Type type, object firstArgument, MethodInfo method)
{ {
return string valueString = valueType.ToString();
#if SIMPLSHARP string message = string.Format("Failed to convert {0} to type {1} - {2}", valueString, type, e.Message);
CDelegate throw new InvalidCastException(message, e);
#else }
Delegate
#endif
.CreateDelegate(type, firstArgument, method);
} }
} }
} }