@@ -43,6 +43,12 @@ public static class GenerationExtensions
43
43
"class_initialize"
44
44
} ;
45
45
46
+ private static readonly List < string > _defaultUsings = new List < string > ( )
47
+ {
48
+ "System.Collections.Generic" ,
49
+ "System"
50
+ } ;
51
+
46
52
private static readonly Dictionary < string , string > _csharpTypeAlias = new Dictionary < string , string > ( 16 )
47
53
{
48
54
{ "System.Int16" , "short" } ,
@@ -99,6 +105,19 @@ public static string ToSafeName(this string name, CodeLanguage language = CodeLa
99
105
100
106
public static string ToType ( this Type type , CodeLanguage language = CodeLanguage . CSharp )
101
107
{
108
+ if ( type . IsGenericType )
109
+ {
110
+ var genericType = type . GetGenericTypeDefinition ( ) . FullName
111
+ . Split ( '`' ) [ 0 ] ; // trim the `1 bit
112
+
113
+ genericType = ToType ( genericType , language ) ;
114
+
115
+ var elementType = ToType ( type . GetGenericArguments ( ) [ 0 ] . FullName , language ) ;
116
+ return language == CodeLanguage . VisualBasic
117
+ ? $ "{ genericType } (Of { elementType } )"
118
+ : $ "{ genericType } <{ elementType } >";
119
+ }
120
+
102
121
return ToType ( type . FullName , language ) ;
103
122
}
104
123
@@ -110,40 +129,26 @@ public static string ToType(this string type, CodeLanguage language = CodeLangua
110
129
if ( language == CodeLanguage . CSharp && _csharpTypeAlias . TryGetValue ( type , out string t ) )
111
130
return t ;
112
131
113
- // drop system from namespace
114
- string [ ] parts = type . Split ( '.' ) ;
115
- if ( parts . Length == 2 && parts [ 0 ] == "System" )
116
- return parts [ 1 ] ;
132
+ // drop common namespaces
133
+ foreach ( var defaultUsing in _defaultUsings )
134
+ if ( type . StartsWith ( defaultUsing ) )
135
+ return type . Remove ( 0 , defaultUsing . Length + 1 ) ;
117
136
118
137
return type ;
119
138
}
120
139
121
140
public static string ToNullableType ( this Type type , bool isNullable = false , CodeLanguage language = CodeLanguage . CSharp )
122
141
{
123
- return ToNullableType ( type . FullName , isNullable , language ) ;
124
- }
142
+ bool isValueType = type . IsValueType ;
125
143
126
- public static string ToNullableType ( this string type , bool isNullable = false , CodeLanguage language = CodeLanguage . CSharp )
127
- {
128
- bool isValueType = type . IsValueType ( ) ;
129
-
130
- type = type . ToType ( language ) ;
144
+ var typeString = type . ToType ( language ) ;
131
145
132
146
if ( ! isValueType || ! isNullable )
133
- return type ;
147
+ return typeString ;
134
148
135
149
return language == CodeLanguage . VisualBasic
136
- ? $ "Nullable(Of { type } )"
137
- : type + "?" ;
138
- }
139
-
140
- public static bool IsValueType ( this string type )
141
- {
142
- if ( ! type . StartsWith ( "System." ) )
143
- return false ;
144
-
145
- var t = Type . GetType ( type , false ) ;
146
- return t != null && t . IsValueType ;
150
+ ? $ "Nullable(Of { typeString } )"
151
+ : typeString + "?" ;
147
152
}
148
153
149
154
public static string ToLiteral ( this string value )
0 commit comments