Skip to content

Commit ad159bf

Browse files
committed
Handle Generic types such as List<> and NpgsqlRange<>
1 parent ab7c0a8 commit ad159bf

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs

+28-23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public static class GenerationExtensions
4343
"class_initialize"
4444
};
4545

46+
private static readonly List<string> _defaultUsings = new List<string>()
47+
{
48+
"System.Collections.Generic",
49+
"System"
50+
};
51+
4652
private static readonly Dictionary<string, string> _csharpTypeAlias = new Dictionary<string, string>(16)
4753
{
4854
{"System.Int16", "short"},
@@ -99,6 +105,19 @@ public static string ToSafeName(this string name, CodeLanguage language = CodeLa
99105

100106
public static string ToType(this Type type, CodeLanguage language = CodeLanguage.CSharp)
101107
{
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+
102121
return ToType(type.FullName, language);
103122
}
104123

@@ -110,40 +129,26 @@ public static string ToType(this string type, CodeLanguage language = CodeLangua
110129
if (language == CodeLanguage.CSharp && _csharpTypeAlias.TryGetValue(type, out string t))
111130
return t;
112131

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);
117136

118137
return type;
119138
}
120139

121140
public static string ToNullableType(this Type type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
122141
{
123-
return ToNullableType(type.FullName, isNullable, language);
124-
}
142+
bool isValueType = type.IsValueType;
125143

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);
131145

132146
if (!isValueType || !isNullable)
133-
return type;
147+
return typeString;
134148

135149
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 + "?";
147152
}
148153

149154
public static string ToLiteral(this string value)

0 commit comments

Comments
 (0)