Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve native AOT compatibility #222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions src/Ben.Demystifier/EnhancedStackTrace.Frames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static ResolvedMethod GetMethodDisplayString(MethodBase originMethod)
var value = field.GetValue(field);
if (value is Delegate d && d.Target is not null)
{
if (ReferenceEquals(d.Method, originMethod) &&
if (d.Method == originMethod &&
d.Target.ToString() == originMethod.DeclaringType?.ToString())
{
methodDisplayInfo.Name = field.Name;
Expand Down Expand Up @@ -386,7 +386,16 @@ private static bool TryResolveSourceMethod(IEnumerable<MethodBase> candidateMeth
ordinal = null;
foreach (var candidateMethod in candidateMethods)
{
if (candidateMethod.GetMethodBody() is not { } methodBody)
MethodBody? methodBody = null;
try
{
methodBody = candidateMethod.GetMethodBody();
}
catch
{
// Platforms like native AOT don't provide access to IL method bodies
}
if (methodBody == null)
{
continue;
}
Expand Down Expand Up @@ -605,9 +614,15 @@ private static ResolvedParameter GetParameter(ParameterInfo parameter)
{
var customAttribs = parameter.GetCustomAttributes(inherit: false);

#if NET45
var tupleNameAttribute = customAttribs.OfType<Attribute>().FirstOrDefault(a => a.IsTupleElementNameAttribute());

var tupleNames = tupleNameAttribute?.GetTransformerNames();
#else
var tupleNameAttribute = customAttribs.OfType<TupleElementNamesAttribute>().FirstOrDefault();

var tupleNames = tupleNameAttribute?.TransformNames;
#endif

if (tupleNames?.Count > 0)
{
Expand All @@ -628,7 +643,7 @@ private static ResolvedParameter GetParameter(ParameterInfo parameter)
};
}

private static ResolvedParameter GetValueTupleParameter(IList<string> tupleNames, string prefix, string? name, Type parameterType)
private static ResolvedParameter GetValueTupleParameter(IList<string?> tupleNames, string prefix, string? name, Type parameterType)
{
return new ValueTupleResolvedParameter(parameterType, tupleNames)
{
Expand Down
8 changes: 6 additions & 2 deletions src/Ben.Demystifier/Internal/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace System.Diagnostics.Internal
/// </summary>
public static class ReflectionHelper
{
#if NET45
private static PropertyInfo? transformerNamesLazyPropertyInfo;
#endif

/// <summary>
/// Returns true if the <paramref name="type"/> is a value tuple type.
Expand All @@ -22,6 +24,7 @@ public static bool IsValueTuple(this Type type)
return type.Namespace == "System" && type.Name.Contains("ValueTuple`");
}

#if NET45
/// <summary>
/// Returns true if the given <paramref name="attribute"/> is of type <code>TupleElementNameAttribute</code>.
/// </summary>
Expand All @@ -43,12 +46,12 @@ public static bool IsTupleElementNameAttribute(this Attribute attribute)
/// To avoid compile-time dependency hell with System.ValueTuple, this method uses reflection
/// instead of casting the attribute to a specific type.
/// </remarks>
public static IList<string>? GetTransformerNames(this Attribute attribute)
public static IList<string?>? GetTransformerNames(this Attribute attribute)
{
Debug.Assert(attribute.IsTupleElementNameAttribute());

var propertyInfo = GetTransformNamesPropertyInfo(attribute.GetType());
return propertyInfo?.GetValue(attribute) as IList<string>;
return propertyInfo?.GetValue(attribute) as IList<string?>;
}

private static PropertyInfo? GetTransformNamesPropertyInfo(Type attributeType)
Expand All @@ -58,5 +61,6 @@ public static bool IsTupleElementNameAttribute(this Attribute attribute)
#pragma warning restore 8634
() => attributeType.GetProperty("TransformNames", BindingFlags.Instance | BindingFlags.Public)!);
}
#endif
}
}
4 changes: 2 additions & 2 deletions src/Ben.Demystifier/ValueTupleResolvedParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace System.Diagnostics
{
public class ValueTupleResolvedParameter : ResolvedParameter
{
public IList<string> TupleNames { get; }
public IList<string?> TupleNames { get; }

public ValueTupleResolvedParameter(Type resolvedType, IList<string> tupleNames)
public ValueTupleResolvedParameter(Type resolvedType, IList<string?> tupleNames)
: base(resolvedType)
=> TupleNames = tupleNames;

Expand Down
Loading