From what I understand, the code needs to be compiled (the same way VS compiles code) for OverrideMethodAspect to work. But LinqPad compiles the code with Roslyn.
Any ideas on how to get OverrideMethodAspect to work on methods defined in LinqPad?
Here is the working code (for VS) if anyone is interested. (Just add the Metalama.Framework package from nuget - make sure 'include pre-release' is checked)
using Metalama.Framework.Aspects;
public class Program
{
public static void Main(string[] args)
{
TestMethod("foo");
}
[SimpleLog]
public static void TestMethod(string x)
{
Console.WriteLine("Hello, " + x);
}
}
public class SimpleLogAttribute : OverrideMethodAspect
{
public override dynamic OverrideMethod()
{
Console.WriteLine($"Entering Method - first arg value is: {meta.Target.Method.Parameters.First().Value}");
try
{
return meta.Proceed();
}
finally
{
Console.WriteLine($"Leaving {meta.Target.Method}");
}
}
}
As far as I can see, Metalama hooks in at the MSBuild level, replacing the Roslyn assemblies with its own versions that implement aspect rewriting. LINQPad doesn't use MSBuild, so it would need to know about and use Metalama's version of Roslyn, and use that explicitly. This might be viable if Metalama's Roslyn assemblies were binary-compatible with the standard Roslyn assemblies, although (possibly intractable) problems might still remain in handling assembly resolution, isolation and unloading in .NET Core/5/6. These problems have so far prevented the use of source generators in LINQPad 7.