Skip to content

Commit

Permalink
Fix debug information by just using the debug scopes from original me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
Ralf1108 committed Sep 21, 2022
1 parent a6a3f46 commit 9d451fb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ private static void Main()
Debugger.Launch();
Debugger.Break();

var t0 = new WrongDebugVariableNames();
t0.Call();

var t1 = new LogTest();
t1.DoIt(7);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using MethodBoundaryAspect.Fody.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodBoundaryAspect.Fody.UnitTests.TestProgram.NetFramework
{
internal class WrongDebugVariableNames
{
public void Call()
{
DoSomeStuff2(new List<int>{42});
}

[MethodInterceptor]
private void DoSomeStuff2(List<int> youcanseethis)
{
var andThis = youcanseethis.Take(1);
var rand = new Random();
if (rand.NextDouble() > 0.5)
{
var butNotThis = youcanseethis.Take(1);
Console.WriteLine("Larger than 0.5");
Console.WriteLine($"{butNotThis}");
}
else
{
var butNotThis = youcanseethis.Take(1);
Console.WriteLine("Smaller than 0.5");
Console.WriteLine($"{butNotThis}");
}
}
}

public sealed class MethodInterceptor : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("Enter: " + args.Instance.GetType().FullName + "." + args.Method.Name);
}

public override void OnExit(MethodExecutionArgs args)
{
Console.WriteLine("Exit: "+ args.Instance.GetType().FullName +"."+args.Method.Name);
}

public override void OnException(MethodExecutionArgs args)
{
Console.WriteLine(args.Exception);
}
}
}
19 changes: 2 additions & 17 deletions src/MethodBoundaryAspect.Fody/MethodWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static MethodDefinition CloneMethod(MethodDefinition method)
};

foreach (var parameter in method.Parameters)
clonedMethod.Parameters.Add(parameter);
clonedMethod.Parameters.Add(parameter);

foreach (var variable in method.Body.Variables)
clonedMethod.Body.Variables.Add(variable);
Expand Down Expand Up @@ -162,22 +162,7 @@ private static MethodDefinition CloneMethod(MethodDefinition method)
}
}

if (method.DebugInformation.HasSequencePoints)
{
foreach (var sequencePoint in method.DebugInformation.SequencePoints)
clonedMethod.DebugInformation.SequencePoints.Add(sequencePoint);
}

clonedMethod.DebugInformation.Scope = new ScopeDebugInformation(method.Body.Instructions.First(), method.Body.Instructions.Last());

if (method.DebugInformation?.Scope?.Variables != null)
{
foreach (var variableDebugInformation in method.DebugInformation.Scope.Variables)
{
clonedMethod.DebugInformation.Scope.Variables.Add(variableDebugInformation);
}
}

clonedMethod.DebugInformation.Scope = method.DebugInformation.Scope;
return clonedMethod;
}

Expand Down

0 comments on commit 9d451fb

Please sign in to comment.