示例#1
0
 /// <summary>
 /// Attempts to convert the inputObject to the specified type. Returns the default value if the conversion fails.
 /// </summary>
 /// <typeparam name="T">The type to convert to</typeparam>
 /// <param name="inputObject">The object to convert</param>
 /// <param name="defaultValue">The value to return if conversion fails</param>
 /// <returns>The converted or default value</returns>
 public static T AsOrDefault <T>(this IConvertible inputObject, T defaultValue = default)
 {
     try
     {
         return(inputObject.As <T>());
     }
     catch (Exception e) when
         (e is InvalidCastException ||
         e is FormatException ||
         e is OverflowException ||
         e is ArgumentNullException)
     {
         return(defaultValue);
     }
 }