From 5092c943114d82f4b2887d295f5c3e51a60b871d Mon Sep 17 00:00:00 2001 From: Miki Date: Thu, 11 May 2023 22:53:25 +0800 Subject: [PATCH] fix DeclaringType code generate issue #643 --- .../CodeAnalysis/MagicOnionTypeInfo.cs | 5 ++-- .../Utils/TypeExtensions.cs | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/MagicOnion.GeneratorCore/Utils/TypeExtensions.cs diff --git a/src/MagicOnion.GeneratorCore/CodeAnalysis/MagicOnionTypeInfo.cs b/src/MagicOnion.GeneratorCore/CodeAnalysis/MagicOnionTypeInfo.cs index d6bf2b45d..78475c7b3 100644 --- a/src/MagicOnion.GeneratorCore/CodeAnalysis/MagicOnionTypeInfo.cs +++ b/src/MagicOnion.GeneratorCore/CodeAnalysis/MagicOnionTypeInfo.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using MagicOnion.Generator.Utils; using Microsoft.CodeAnalysis; namespace MagicOnion.Generator.CodeAnalysis; @@ -167,7 +168,7 @@ public static MagicOnionTypeInfo CreateFromType(Type type) return CreateEnum(type.Namespace, type.Name, CreateFromType(type.GetEnumUnderlyingType())); } - return Create(type.Namespace, type.Name, Array.Empty(), type.IsValueType); + return Create(type.Namespace, type.GetFullDeclaringTypeName(), Array.Empty(), type.IsValueType); } public static MagicOnionTypeInfo CreateFromSymbol(ITypeSymbol symbol) @@ -186,7 +187,7 @@ public static MagicOnionTypeInfo CreateFromSymbol(ITypeSymbol symbol) if (finalSymbol is INamedTypeSymbol namedTypeSymbol) { var @namespace = finalSymbol.ContainingNamespace.IsGlobalNamespace ? string.Empty : finalSymbol.ContainingNamespace.ToDisplayString(); - var name = finalSymbol.Name; + var name = finalSymbol.GetFullDeclaringTypeName(); var typeArguments = namedTypeSymbol.TypeArguments.Select(MagicOnionTypeInfo.CreateFromSymbol).ToArray(); MagicOnionTypeInfo type; diff --git a/src/MagicOnion.GeneratorCore/Utils/TypeExtensions.cs b/src/MagicOnion.GeneratorCore/Utils/TypeExtensions.cs new file mode 100644 index 000000000..c25d62b86 --- /dev/null +++ b/src/MagicOnion.GeneratorCore/Utils/TypeExtensions.cs @@ -0,0 +1,30 @@ +using System.Text; +using Microsoft.CodeAnalysis; + +namespace MagicOnion.Generator.Utils; + +internal static class TypeExtensions +{ + public static string GetFullDeclaringTypeName(this Type type) + { + var typeNameParts = new List(); + while (type != null) + { + typeNameParts.Insert(0, type.Name); + type = type.DeclaringType; + } + return string.Join(".", typeNameParts); + } + + public static string GetFullDeclaringTypeName(this ITypeSymbol typeSymbol) + { + return typeSymbol.ToDisplayString( + new SymbolDisplayFormat( + SymbolDisplayGlobalNamespaceStyle.OmittedAsContaining, + SymbolDisplayTypeQualificationStyle.NameAndContainingTypes, + SymbolDisplayGenericsOptions.None, + miscellaneousOptions: SymbolDisplayMiscellaneousOptions.ExpandNullable + )); + } + +}