Skip to content

Commit

Permalink
Fix TypeExtensions ToTypeString method Exception.
Browse files Browse the repository at this point in the history
Fix TypeExtensions ToTypeString method throws if invoked on a generic class without specify the generic type

Closes #609
  • Loading branch information
GabrieleMessina committed Mar 11, 2023
1 parent 1537e98 commit 354e56f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/CommunityToolkit.Diagnostics/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommunityToolkit.Diagnostics.UnitTests.Extensions.T>", typeof(Animal.Rabbit<>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<int?>", typeof(Animal.Rabbit<int?>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<string>", typeof(Animal.Rabbit<string>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Dog>", typeof(Animal.Rabbit<Animal.Dog>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<int>.Foo", typeof(Animal.Rabbit<int>.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<int>.Foo<string>", typeof(Animal.Rabbit<int>.Foo<string>))]
Expand Down

0 comments on commit 354e56f

Please sign in to comment.