Improved support for Nullable<T> and dynamic parameters (#96)
This commit is contained in:
parent
1b99d61cf3
commit
1ca8f79a36
@ -563,6 +563,7 @@ namespace System.Diagnostics
|
|||||||
Prefix = prefix,
|
Prefix = prefix,
|
||||||
Name = parameter.Name,
|
Name = parameter.Name,
|
||||||
ResolvedType = parameterType,
|
ResolvedType = parameterType,
|
||||||
|
IsDynamicType = parameter.IsDefined(typeof(DynamicAttribute), false)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@ namespace System.Diagnostics
|
|||||||
public Type ResolvedType { get; set; }
|
public Type ResolvedType { get; set; }
|
||||||
|
|
||||||
public string Prefix { get; set; }
|
public string Prefix { get; set; }
|
||||||
|
public bool IsDynamicType { get; set; }
|
||||||
|
|
||||||
public override string ToString() => Append(new StringBuilder()).ToString();
|
public override string ToString() => Append(new StringBuilder()).ToString();
|
||||||
|
|
||||||
@ -23,7 +24,11 @@ namespace System.Diagnostics
|
|||||||
.Append(" ");
|
.Append(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ResolvedType != null)
|
if (IsDynamicType)
|
||||||
|
{
|
||||||
|
sb.Append("dynamic");
|
||||||
|
}
|
||||||
|
else if (ResolvedType != null)
|
||||||
{
|
{
|
||||||
AppendTypeName(sb);
|
AppendTypeName(sb);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,10 +68,19 @@ namespace System.Diagnostics
|
|||||||
private static void ProcessType(StringBuilder builder, Type type, DisplayNameOptions options)
|
private static void ProcessType(StringBuilder builder, Type type, DisplayNameOptions options)
|
||||||
{
|
{
|
||||||
if (type.IsGenericType)
|
if (type.IsGenericType)
|
||||||
|
{
|
||||||
|
var underlyingType = Nullable.GetUnderlyingType(type);
|
||||||
|
if (underlyingType != null)
|
||||||
|
{
|
||||||
|
ProcessType(builder, underlyingType, options);
|
||||||
|
builder.Append('?');
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
var genericArguments = type.GetGenericArguments();
|
var genericArguments = type.GetGenericArguments();
|
||||||
ProcessGenericType(builder, type, genericArguments, genericArguments.Length, options);
|
ProcessGenericType(builder, type, genericArguments, genericArguments.Length, options);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (type.IsArray)
|
else if (type.IsArray)
|
||||||
{
|
{
|
||||||
ProcessArrayType(builder, type, options);
|
ProcessArrayType(builder, type, options);
|
||||||
|
|||||||
65
test/Ben.Demystifier.Test/MethodTests.cs
Normal file
65
test/Ben.Demystifier.Test/MethodTests.cs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
namespace Ben.Demystifier.Test
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class MethodTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void DemistifiesMethodWithNullableInt()
|
||||||
|
{
|
||||||
|
Exception dex = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MethodWithNullableInt(1);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
dex = e.Demystify();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var stackTrace = dex.ToString();
|
||||||
|
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
|
||||||
|
var trace = string.Join(string.Empty, stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
|
||||||
|
|
||||||
|
var expected = string.Join(string.Empty, new[] {
|
||||||
|
"System.ArgumentException: Value does not fall within the expected range.",
|
||||||
|
" at bool Ben.Demystifier.Test.MethodTests.MethodWithNullableInt(int? number)",
|
||||||
|
" at void Ben.Demystifier.Test.MethodTests.DemistifiesMethodWithNullableInt()"});
|
||||||
|
|
||||||
|
Assert.Equal(expected, trace);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DemistifiesMethodWithDynamic()
|
||||||
|
{
|
||||||
|
Exception dex = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MethodWithDynamic(1);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
dex = e.Demystify();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var stackTrace = dex.ToString();
|
||||||
|
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
|
||||||
|
var trace = string.Join(string.Empty, stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
|
||||||
|
|
||||||
|
var expected = string.Join(string.Empty, new[] {
|
||||||
|
"System.ArgumentException: Value does not fall within the expected range.",
|
||||||
|
" at bool Ben.Demystifier.Test.MethodTests.MethodWithDynamic(dynamic value)",
|
||||||
|
" at void Ben.Demystifier.Test.MethodTests.DemistifiesMethodWithDynamic()"});
|
||||||
|
|
||||||
|
Assert.Equal(expected, trace);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool MethodWithNullableInt(int? number) => throw new ArgumentException();
|
||||||
|
|
||||||
|
private bool MethodWithDynamic(dynamic value) => throw new ArgumentException();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user