使用 Metalama 记录 LinqPad 7 C# (.NET Core/5/6) 中的参数值
据我了解,需要对代码进行编译(以相同的方式与编译代码)OverrideMethodaspect
工作。但是LinqPad用Roslyn编译了代码。
关于如何获得OverrideMethodaspect
的任何想法,可以处理LINQPAD中定义的方法?
如果有人有兴趣,这是工作代码(vs)。 (只需添加Nuget的Metalama.framework
软件包 - 确保检查“ Include Pre -Release”)
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}");
}
}
}
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(quot;Entering Method - first arg value is: {meta.Target.Method.Parameters.First().Value}");
try
{
return meta.Proceed();
}
finally
{
Console.WriteLine(quot;Leaving {meta.Target.Method}");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,Metalama 在 MSBuild 级别挂钩,用它自己的实现切面重写的版本替换 Roslyn 程序集。 LINQPad 不使用 MSBuild,因此它需要了解并使用 Metalama 的 Roslyn 版本,并明确使用它。如果 Metalama 的 Roslyn 程序集与标准 Roslyn 程序集二进制兼容,这可能是可行的,尽管在 .NET Core/5/6 中处理程序集解析、隔离和卸载时可能仍然存在(可能棘手的)问题。到目前为止,这些问题阻碍了源生成器在 LINQPad 7 中的使用。
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.