Skip to content

Commit

Permalink
fix DeclaringType code generate issue Cysharp#643
Browse files Browse the repository at this point in the history
  • Loading branch information
sableangle committed May 11, 2023
1 parent c5cb55b commit 5092c94
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using MagicOnion.Generator.Utils;
using Microsoft.CodeAnalysis;

namespace MagicOnion.Generator.CodeAnalysis;
Expand Down Expand Up @@ -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<MagicOnionTypeInfo>(), type.IsValueType);
return Create(type.Namespace, type.GetFullDeclaringTypeName(), Array.Empty<MagicOnionTypeInfo>(), type.IsValueType);
}

public static MagicOnionTypeInfo CreateFromSymbol(ITypeSymbol symbol)
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/MagicOnion.GeneratorCore/Utils/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
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
));
}

}

0 comments on commit 5092c94

Please sign in to comment.