Fix stack trace for tuples type as part of a generic type.

Fix for #57
This commit is contained in:
Sergey Tepliakov
2018-02-20 14:59:05 -08:00
parent 57e2d49ad1
commit 54f4b0c24b
9 changed files with 196 additions and 22 deletions

View File

@@ -19,7 +19,6 @@ namespace System.Diagnostics
{
public partial class EnhancedStackTrace
{
private static List<EnhancedStackFrame> GetFrames(Exception exception)
{
if (exception == null)
@@ -516,7 +515,7 @@ namespace System.Diagnostics
{
foreach (var attrib in attribs)
{
if (attrib is Attribute att && att.GetType().Namespace == "System.Runtime.CompilerServices" && att.GetType().Name == "IsReadOnlyAttribute")
if (attrib is Attribute att && att.GetType().IsReadOnlyAttribute())
{
return "in";
}
@@ -579,6 +578,32 @@ namespace System.Diagnostics
}
private static ResolvedParameter GetValueTupleParameter(IList<string> tupleNames, string prefix, string name, Type parameterType)
{
string typeName;
if (parameterType.IsValueTuple())
{
typeName = GetValueTupleParameterName(tupleNames, parameterType);
}
else
{
// Need to unwrap the first generic argument first.
var genericTypeName = TypeNameHelper.GetTypeNameForGenericType(parameterType);
var valueTupleFullName = GetValueTupleParameterName(tupleNames, parameterType.GetGenericArguments()[0]);
typeName = $"{genericTypeName}<{valueTupleFullName}>";
}
return new ResolvedParameter
{
Prefix = prefix,
Name = name,
Type = typeName,
ResolvedType = parameterType,
};
}
private static string GetValueTupleParameterName(IList<string> tupleNames, Type parameterType)
{
var sb = new StringBuilder();
sb.Append("(");
@@ -608,14 +633,7 @@ namespace System.Diagnostics
}
sb.Append(")");
return new ResolvedParameter
{
Prefix = prefix,
Name = name,
Type = sb.ToString(),
ResolvedType = parameterType,
};
return sb.ToString();
}
private static bool ShowInStackTrace(MethodBase method)

View File

@@ -0,0 +1,30 @@
// Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Reflection;
using System.Reflection.Emit;
namespace System.Diagnostics.Internal
{
/// <summary>
/// A helper class that contains utilities methods for dealing with reflection.
/// </summary>
public static class ReflectionHelper
{
/// <summary>
/// Returns true if <paramref name="type"/> is <code>System.Runtime.CompilerServices.IsReadOnlyAttribute</code>.
/// </summary>
public static bool IsReadOnlyAttribute(this Type type)
{
return type.Namespace == "System.Runtime.CompilerServices" && type.Name == "IsReadOnlyAttribute";
}
/// <summary>
/// Returns true if the <paramref name="type"/> is a value tuple type.
/// </summary>
public static bool IsValueTuple(this Type type)
{
return type.Namespace == "System" && type.Name.Contains("ValueTuple`");
}
}
}

View File

@@ -43,6 +43,22 @@ namespace System.Diagnostics.Internal
return builder.ToString();
}
/// <summary>
/// Returns a name of given generic type without '`'.
/// </summary>
public static string GetTypeNameForGenericType(Type type)
{
if (!type.IsGenericType)
{
throw new ArgumentException("The given type should be generic", nameof(type));
}
var genericPartIndex = type.Name.IndexOf('`');
Debug.Assert(genericPartIndex >= 0);
return type.Name.Substring(0, genericPartIndex);
}
private static void ProcessType(StringBuilder builder, Type type, DisplayNameOptions options)
{
if (type.IsGenericType)