使用 Metalama 记录 LinqPad 7 C# (.NET Core/5/6) 中的参数值

发布于 2025-01-20 02:58:39 字数 993 浏览 2 评论 0原文

据我了解,需要对代码进行编译(以相同的方式与编译代码)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2025-01-27 02:58:39

据我所知,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文