@@ -24,16 +24,44 @@ public static T ConvertTo<T>(this object from)
24
24
if ( from == null )
25
25
return default ( T ) ;
26
26
27
- if ( from . GetType ( ) == typeof ( T ) )
27
+ var fromType = from . GetType ( ) ;
28
+ if ( fromType == typeof ( T ) )
28
29
return ( T ) from ;
29
30
30
- if ( from . GetType ( ) . IsValueType ( ) || typeof ( T ) . IsValueType ( ) )
31
+ if ( fromType . IsValueType ( ) || typeof ( T ) . IsValueType ( ) )
32
+ {
33
+ if ( ! fromType . IsEnum ( ) && ! typeof ( T ) . IsEnum ( ) )
34
+ {
35
+ if ( typeof ( T ) == typeof ( char ) && from is string s )
36
+ return ( T ) ( s . Length > 0 ? ( object ) s [ 0 ] : null ) ;
37
+ if ( typeof ( T ) == typeof ( string ) && from is char c )
38
+ return ( T ) ( object ) c . ToString ( ) ;
39
+
40
+ var destNumberType = DynamicNumber . GetNumber ( typeof ( T ) ) ;
41
+ var value = destNumberType ? . ConvertFrom ( from ) ;
42
+ if ( value != null )
43
+ {
44
+ if ( typeof ( T ) == typeof ( char ) )
45
+ return ( T ) ( object ) value . ToString ( ) [ 0 ] ;
46
+
47
+ return ( T ) value ;
48
+ }
49
+
50
+ if ( typeof ( T ) == typeof ( string ) )
51
+ {
52
+ var srcNumberType = DynamicNumber . GetNumber ( from . GetType ( ) ) ;
53
+ if ( srcNumberType != null )
54
+ return ( T ) ( object ) srcNumberType . ToString ( from ) ;
55
+ }
56
+ }
57
+
31
58
return ( T ) ChangeValueType ( from , typeof ( T ) ) ;
59
+ }
32
60
33
61
if ( typeof ( IEnumerable ) . IsAssignableFromType ( typeof ( T ) ) )
34
62
{
35
63
var listResult = TranslateListWithElements . TryTranslateCollections (
36
- from . GetType ( ) , typeof ( T ) , from ) ;
64
+ fromType , typeof ( T ) , from ) ;
37
65
38
66
return ( T ) listResult ;
39
67
}
0 commit comments