From bfb9c2ff607bf2e90d341cf44379bd7865e41a69 Mon Sep 17 00:00:00 2001 From: David Boone Date: Tue, 11 Jun 2024 11:48:32 -0700 Subject: [PATCH] Handle Generic types such as List<> and NpgsqlRange<> --- .../Extensions/GenerationExtensions.cs | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs b/src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs index d6e9280b..3b5c44cd 100644 --- a/src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs @@ -43,6 +43,12 @@ public static class GenerationExtensions "class_initialize" }; + private static readonly List _defaultUsings = new List() + { + "System.Collections.Generic", + "System" + }; + private static readonly Dictionary _csharpTypeAlias = new Dictionary(16) { {"System.Int16", "short"}, @@ -99,6 +105,19 @@ public static string ToSafeName(this string name, CodeLanguage language = CodeLa public static string ToType(this Type type, CodeLanguage language = CodeLanguage.CSharp) { + if (type.IsGenericType) + { + var genericType = type.GetGenericTypeDefinition().FullName + .Split('`')[0]; // trim the `1 bit + + genericType = ToType(genericType, language); + + var elementType = ToType(type.GetGenericArguments()[0].FullName, language); + return language == CodeLanguage.VisualBasic + ? $"{genericType}(Of {elementType})" + : $"{genericType}<{elementType}>"; + } + return ToType(type.FullName, language); } @@ -110,40 +129,26 @@ public static string ToType(this string type, CodeLanguage language = CodeLangua if (language == CodeLanguage.CSharp && _csharpTypeAlias.TryGetValue(type, out string t)) return t; - // drop system from namespace - string[] parts = type.Split('.'); - if (parts.Length == 2 && parts[0] == "System") - return parts[1]; + // drop common namespaces + foreach (var defaultUsing in _defaultUsings) + if (type.StartsWith(defaultUsing)) + return type.Remove(0, defaultUsing.Length + 1); return type; } public static string ToNullableType(this Type type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp) { - return ToNullableType(type.FullName, isNullable, language); - } + bool isValueType = type.IsValueType; - public static string ToNullableType(this string type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp) - { - bool isValueType = type.IsValueType(); - - type = type.ToType(language); + var typeString = type.ToType(language); if (!isValueType || !isNullable) - return type; + return typeString; return language == CodeLanguage.VisualBasic - ? $"Nullable(Of {type})" - : type + "?"; - } - - public static bool IsValueType(this string type) - { - if (!type.StartsWith("System.")) - return false; - - var t = Type.GetType(type, false); - return t != null && t.IsValueType; + ? $"Nullable(Of {typeString})" + : typeString + "?"; } public static string ToLiteral(this string value)