BadScript 2
Loading...
Searching...
No Matches
CommandLine.CastExtensions Class Reference

Static Public Member Functions

static bool CanCast< T > (this Type baseType)
 
static bool CanCast< T > (this object obj)
 
static T Cast< T > (this object obj)
 

Static Private Member Functions

static bool CanImplicitCast< T > (this Type baseType)
 
static bool CanImplicitCast< T > (this object obj)
 
static bool CanExplicitCast< T > (this Type baseType)
 
static bool CanExplicitCast< T > (this object obj)
 
static bool CanCast< T > (this Type baseType, string castMethodName)
 
static T ImplicitCast< T > (this object obj)
 
static T ExplicitCast< T > (this object obj)
 
static T Cast< T > (this object obj, string castMethodName)
 

Static Private Attributes

const string ImplicitCastMethodName = "op_Implicit"
 
const string ExplicitCastMethodName = "op_Explicit"
 

Detailed Description

Definition at line 7 of file CastExtensions.cs.

Member Function Documentation

◆ 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

Definition at line 58 of file CastExtensions.cs.

59 {
60 return baseType.CanCast<T>(ExplicitCastMethodName);
61 }
const string ExplicitCastMethodName

◆ 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

Definition at line 46 of file CastExtensions.cs.

47 {
48 return baseType.CanCast<T>(ImplicitCastMethodName);
49 }
const string ImplicitCastMethodName

◆ 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

Definition at line 91 of file CastExtensions.cs.

92 {
93 return obj.Cast<T>(ExplicitCastMethodName);
94 }

◆ ImplicitCast< T >()

static T CommandLine.CastExtensions.ImplicitCast< T > ( this object  obj)
staticprivate

Definition at line 86 of file CastExtensions.cs.

87 {
88 return obj.Cast<T>(ImplicitCastMethodName);
89 }

Member Data Documentation

◆ ExplicitCastMethodName

const string CommandLine.CastExtensions.ExplicitCastMethodName = "op_Explicit"
staticprivate

Definition at line 10 of file CastExtensions.cs.

◆ ImplicitCastMethodName

const string CommandLine.CastExtensions.ImplicitCastMethodName = "op_Implicit"
staticprivate

Definition at line 9 of file CastExtensions.cs.


The documentation for this class was generated from the following file: