Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Vertical/CommandLine/Conversion/ConversionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ internal ConversionException(string context, Type targetType, string argumentVal
// Formats the exception message.
private static string FormatMessage(string context, Type targetType, string argumentValue)
{
return $"{context}: could not convert {Formatting.Quote(argumentValue)} to target type {targetType.Name}.";
var friendlyTargetType = TypeHelpers.GetFriendlyDisplayName(targetType);

return $"{context}: could not convert {Formatting.Quote(argumentValue)} to target type {friendlyTargetType}.";
}
}
}
23 changes: 23 additions & 0 deletions src/Vertical/CommandLine/Infrastructure/TypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,28 @@ internal static string GetGenericTypeName(string typeName)
var markerIndex = typeName.IndexOf('`');
return markerIndex > -1 ? typeName.Substring(0, markerIndex) : typeName;
}

internal static string GetFriendlyDisplayName(Type type)
{
try
{
if (!type.IsGenericType)
return type.Name;

var genericTypeName = type.GetGenericTypeDefinition().Name;
var tickIndex = genericTypeName.IndexOf('`');
var trimmedTypeName = genericTypeName.Substring(0, tickIndex);
var typeParams = type
.GetGenericArguments()
.Select(GetFriendlyDisplayName);
var typeParamString = string.Join(",", typeParams);

return $"{trimmedTypeName}<{typeParamString}>";
}
catch
{
return type.Name;
}
}
}
}
11 changes: 11 additions & 0 deletions test/Infrastructure/TypeHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// or refer to https://opensource.org/licenses/MIT

using System;
using System.Collections.Generic;
using Shouldly;
using Vertical.CommandLine.Infrastructure;
using Xunit;
Expand All @@ -20,5 +21,15 @@ public void GetKnownMethodInfoThrowsForInvalidMethod()
Array.Empty<Type>(),
typeof(void)));
}

[Theory]
[InlineData(typeof(string), "String")]
[InlineData(typeof(int), "Int32")]
[InlineData(typeof(int?), "Nullable<Int32>")]
[InlineData(typeof(Dictionary<int?, string>), "Dictionary<Nullable<Int32>,String>")]
public void GetFriendlyNameReturnsExpected(Type type, string expected)
{
TypeHelpers.GetFriendlyDisplayName(type).ShouldBe(expected);
}
}
}