Definition at line 7 of file CastExtensions.cs.
◆ CanCast< T >() [1/3]
static bool CommandLine.CastExtensions.CanCast< T > |
( |
this object |
obj | ) |
|
|
static |
Definition at line 17 of file CastExtensions.cs.
18 {
19 Type objType = obj.GetType();
20
21 return objType.CanCast<T>();
22 }
◆ CanCast< T >() [2/3]
static bool CommandLine.CastExtensions.CanCast< T > |
( |
this Type |
baseType | ) |
|
|
static |
Definition at line 12 of file CastExtensions.cs.
13 {
14 return baseType.CanImplicitCast<T>() || baseType.CanExplicitCast<T>();
15 }
◆ CanCast< T >() [3/3]
static bool CommandLine.CastExtensions.CanCast< T > |
( |
this Type |
baseType, |
|
|
string |
castMethodName |
|
) |
| |
|
staticprivate |
Definition at line 70 of file CastExtensions.cs.
71 {
72 Type targetType = typeof(T);
73
74 return baseType.GetMethods(BindingFlags.Public | BindingFlags.Static)
75 .Where(mi => mi.Name == castMethodName && mi.ReturnType == targetType)
76 .Any(mi =>
77 {
78 ParameterInfo pi = mi.GetParameters()
79 .FirstOrDefault();
80
81 return pi != null && pi.ParameterType == baseType;
82 }
83 );
84 }
◆ CanExplicitCast< T >() [1/2]
static bool CommandLine.CastExtensions.CanExplicitCast< T > |
( |
this object |
obj | ) |
|
|
staticprivate |
Definition at line 63 of file CastExtensions.cs.
64 {
65 Type baseType = obj.GetType();
66
67 return baseType.CanExplicitCast<T>();
68 }
◆ CanExplicitCast< T >() [2/2]
static bool CommandLine.CastExtensions.CanExplicitCast< T > |
( |
this Type |
baseType | ) |
|
|
staticprivate |
◆ CanImplicitCast< T >() [1/2]
static bool CommandLine.CastExtensions.CanImplicitCast< T > |
( |
this object |
obj | ) |
|
|
staticprivate |
Definition at line 51 of file CastExtensions.cs.
52 {
53 Type baseType = obj.GetType();
54
55 return baseType.CanImplicitCast<T>();
56 }
◆ CanImplicitCast< T >() [2/2]
static bool CommandLine.CastExtensions.CanImplicitCast< T > |
( |
this Type |
baseType | ) |
|
|
staticprivate |
◆ Cast< T >() [1/2]
static T CommandLine.CastExtensions.Cast< T > |
( |
this object |
obj | ) |
|
|
static |
Definition at line 24 of file CastExtensions.cs.
25 {
26 try
27 {
28 return (T)obj;
29 }
30 catch (InvalidCastException)
31 {
32 if (obj.CanImplicitCast<T>())
33 {
34 return obj.ImplicitCast<T>();
35 }
36
37 if (obj.CanExplicitCast<T>())
38 {
39 return obj.ExplicitCast<T>();
40 }
41
42 throw;
43 }
44 }
◆ Cast< T >() [2/2]
static T CommandLine.CastExtensions.Cast< T > |
( |
this object |
obj, |
|
|
string |
castMethodName |
|
) |
| |
|
staticprivate |
Definition at line 96 of file CastExtensions.cs.
97 {
98 Type objType = obj.GetType();
99
100 MethodInfo conversionMethod = objType.GetMethods(BindingFlags.Public | BindingFlags.Static)
101 .Where(mi => mi.Name == castMethodName && mi.ReturnType == typeof(T))
102 .SingleOrDefault(mi =>
103 {
104 ParameterInfo pi = mi.GetParameters()
105 .FirstOrDefault();
106
107 return pi != null && pi.ParameterType == objType;
108 }
109 );
110
111 if (conversionMethod != null)
112 {
113 return (T)conversionMethod.Invoke(null,
114 new[] { obj }
115 );
116 }
117
118 throw new InvalidCastException($"No method to cast {objType.FullName} to {typeof(T).FullName}");
119 }
◆ ExplicitCast< T >()
static T CommandLine.CastExtensions.ExplicitCast< T > |
( |
this object |
obj | ) |
|
|
staticprivate |
◆ ImplicitCast< T >()
static T CommandLine.CastExtensions.ImplicitCast< T > |
( |
this object |
obj | ) |
|
|
staticprivate |
◆ ExplicitCastMethodName
const string CommandLine.CastExtensions.ExplicitCastMethodName = "op_Explicit" |
|
staticprivate |
◆ ImplicitCastMethodName
const string CommandLine.CastExtensions.ImplicitCastMethodName = "op_Implicit" |
|
staticprivate |
The documentation for this class was generated from the following file: