From 354e56f5f99c0a013d0efe9958c6d55d184b3a20 Mon Sep 17 00:00:00 2001 From: Gabriele Messina Date: Sat, 11 Mar 2023 16:55:49 +0100 Subject: [PATCH] Fix TypeExtensions ToTypeString method Exception. Fix TypeExtensions ToTypeString method throws if invoked on a generic class without specify the generic type Closes #609 --- .../Extensions/TypeExtensions.cs | 6 ++++-- .../Extensions/Test_TypeExtensions.cs | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/CommunityToolkit.Diagnostics/Extensions/TypeExtensions.cs b/src/CommunityToolkit.Diagnostics/Extensions/TypeExtensions.cs index fb6942ba..041584f4 100644 --- a/src/CommunityToolkit.Diagnostics/Extensions/TypeExtensions.cs +++ b/src/CommunityToolkit.Diagnostics/Extensions/TypeExtensions.cs @@ -161,8 +161,10 @@ private static string FormatDisplayString(Type type, int genericTypeOffset, Read displayName = type.Name; } - // If the type is nested, recursively format the hierarchy as well - if (type.IsNested) + // If the type is nested, recursively format the hierarchy as well, + // unless the type is a generic type parameter, in that case type.DeclaringType returns the parent class that defined the generic type parameter, + // however the current execution of FormatDisplayString it's been invoked recursively while trying to format the parent class so we need to skip this recursive call. + if (type.IsNested && !type.IsGenericParameter) { return $"{FormatDisplayString(type.DeclaringType!, genericTypeOffset, typeArguments)}.{displayName}"; } diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Extensions/Test_TypeExtensions.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Extensions/Test_TypeExtensions.cs index b86e8782..fb0aa6d5 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Extensions/Test_TypeExtensions.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Extensions/Test_TypeExtensions.cs @@ -45,8 +45,10 @@ public void Test_TypeExtensions_GenericTypes(string name, Type type) [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal", typeof(Animal))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Cat", typeof(Animal.Cat))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Dog", typeof(Animal.Dog))] + [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit", typeof(Animal.Rabbit<>))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit", typeof(Animal.Rabbit))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit", typeof(Animal.Rabbit))] + [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit", typeof(Animal.Rabbit))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit.Foo", typeof(Animal.Rabbit.Foo))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<(string, int)?>.Foo", typeof(Animal.Rabbit<(string, int)?>.Foo))] [DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit.Foo", typeof(Animal.Rabbit.Foo))]