|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace CommandLine |
| 6 | +{ |
| 7 | + public static class CastExtensions |
| 8 | + { |
| 9 | + private const string ImplicitCastMethodName = "op_Implicit"; |
| 10 | + private const string ExplicitCastMethodName = "op_Explicit"; |
| 11 | + |
| 12 | + public static bool CanCast<T>(this Type baseType) |
| 13 | + { |
| 14 | + return baseType.CanImplicitCast<T>() || baseType.CanExplicitCast<T>(); |
| 15 | + } |
| 16 | + |
| 17 | + public static bool CanCast<T>(this object obj) |
| 18 | + { |
| 19 | + var objType = obj.GetType(); |
| 20 | + return objType.CanCast<T>(); |
| 21 | + } |
| 22 | + |
| 23 | + public static T Cast<T>(this object obj) |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + return (T) obj; |
| 28 | + } |
| 29 | + catch (InvalidCastException) |
| 30 | + { |
| 31 | + if (obj.CanImplicitCast<T>()) |
| 32 | + return obj.ImplicitCast<T>(); |
| 33 | + if (obj.CanExplicitCast<T>()) |
| 34 | + return obj.ExplicitCast<T>(); |
| 35 | + else |
| 36 | + throw; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static bool CanImplicitCast<T>(this Type baseType) |
| 41 | + { |
| 42 | + return baseType.CanCast<T>(ImplicitCastMethodName); |
| 43 | + } |
| 44 | + |
| 45 | + private static bool CanImplicitCast<T>(this object obj) |
| 46 | + { |
| 47 | + var baseType = obj.GetType(); |
| 48 | + return baseType.CanImplicitCast<T>(); |
| 49 | + } |
| 50 | + |
| 51 | + private static bool CanExplicitCast<T>(this Type baseType) |
| 52 | + { |
| 53 | + return baseType.CanCast<T>(ExplicitCastMethodName); |
| 54 | + } |
| 55 | + |
| 56 | + private static bool CanExplicitCast<T>(this object obj) |
| 57 | + { |
| 58 | + var baseType = obj.GetType(); |
| 59 | + return baseType.CanExplicitCast<T>(); |
| 60 | + } |
| 61 | + |
| 62 | + private static bool CanCast<T>(this Type baseType, string castMethodName) |
| 63 | + { |
| 64 | + var targetType = typeof(T); |
| 65 | + return baseType.GetMethods(BindingFlags.Public | BindingFlags.Static) |
| 66 | + .Where(mi => mi.Name == castMethodName && mi.ReturnType == targetType) |
| 67 | + .Any(mi => |
| 68 | + { |
| 69 | + ParameterInfo pi = mi.GetParameters().FirstOrDefault(); |
| 70 | + return pi != null && pi.ParameterType == baseType; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + private static T ImplicitCast<T>(this object obj) |
| 75 | + { |
| 76 | + return obj.Cast<T>(ImplicitCastMethodName); |
| 77 | + } |
| 78 | + |
| 79 | + private static T ExplicitCast<T>(this object obj) |
| 80 | + { |
| 81 | + return obj.Cast<T>(ExplicitCastMethodName); |
| 82 | + } |
| 83 | + |
| 84 | + private static T Cast<T>(this object obj, string castMethodName) |
| 85 | + { |
| 86 | + var objType = obj.GetType(); |
| 87 | + MethodInfo conversionMethod = objType.GetMethods(BindingFlags.Public | BindingFlags.Static) |
| 88 | + .Where(mi => mi.Name == castMethodName && mi.ReturnType == typeof(T)) |
| 89 | + .SingleOrDefault(mi => |
| 90 | + { |
| 91 | + ParameterInfo pi = mi.GetParameters().FirstOrDefault(); |
| 92 | + return pi != null && pi.ParameterType == objType; |
| 93 | + }); |
| 94 | + if (conversionMethod != null) |
| 95 | + return (T) conversionMethod.Invoke(null, new[] {obj}); |
| 96 | + else |
| 97 | + throw new InvalidCastException($"No method to cast {objType.FullName} to {typeof(T).FullName}"); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments