Moles Isolation框架是如何实现的?

发布于 2024-09-05 10:02:35 字数 597 浏览 7 评论 0 原文

Moles 是 Microsoft 创建的隔离框架。 Moles 的一个很酷的功能是它可以“模拟”静态/非虚拟方法和密封类(这对于像 Moq 这样的框架来说是不可能的)。下面是 Moles 功能的快速演示:

Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now);

// MDateTime is part of Moles; the below will "override" DateTime.Now's behavior
MDateTime.NowGet = () => new DateTime(2012, 1, 1); 
Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now);

看起来 Moles 能够在运行时修改诸如 DateTime.Now 之类的 CIL 主体。由于 Moles 不是开源的,我很好奇 Moles 使用哪种机制来在运行时修改方法的 CIL。任何人都可以阐明吗?

Moles is an isolation framework created by Microsoft. A cool feature of Moles is that it can "mock" static/non-virtual methods and sealed classes (which is not possible with frameworks like Moq). Below is the quick demonstration of what Moles can do:

Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now);

// MDateTime is part of Moles; the below will "override" DateTime.Now's behavior
MDateTime.NowGet = () => new DateTime(2012, 1, 1); 
Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now);

Seems like Moles is able to modify the CIL body of things like DateTime.Now at runtime. Since Moles isn't open-source, I'm curious to know which mechanism Moles uses in order to modify methods' CIL at runtime. Can anyone shed any light?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦途 2024-09-12 10:02:35

Moles 实现了一个 CLR 探查器(特别是 ICorProfilerCallback 接口),该接口允许在 MSIL 方法体被 .NET 运行时编译为汇编代码之前重写它们。这是通过 JitCompileStarted 回调完成的。

在每个方法中,Moles 引入了一个如下所示的绕道:

static struct DateTime 
{
    static DateTime Now
    {
        get 
        {
            Func<DateTime> d = __Detours.GetDelegate(
                null, // this point null in static methods
                methodof(here) // current method token
                );
            if(d != null)
                return d();
            ... // original body
        }
    }
}

当您设置摩尔时,您的委托将存储在底层 __Detours 字典中,每当执行该方法时都会查找该字典。

Moles implements a CLR profiler (in particular the ICorProfilerCallback interface) that allows to rewrite MSIL method bodies before they are compiled into assembly code by the .NET runtime. This is done in particular through the JitCompileStarted callback.

In each method, Moles introduces a detour that looks like this:

static struct DateTime 
{
    static DateTime Now
    {
        get 
        {
            Func<DateTime> d = __Detours.GetDelegate(
                null, // this point null in static methods
                methodof(here) // current method token
                );
            if(d != null)
                return d();
            ... // original body
        }
    }
}

When you set a mole, your delegate is stored in the underlying __Detours dictionary which gets looked up whenver the method is executed.

生生漫 2024-09-12 10:02:35

这就像您想要的任何程序集的包装器一样,例如 mscorlib(此示例基于 mscorlib 的 Moles 程序集包装器)。这使您能够用编码器编写的委托替换任何 .NET 方法

这不是自动工作的。在此开始工作之前,您必须首先创建 Moles XML 配置 文件,其中包含“包装器”的程序集列表,并通过此代码 Moles 从配置文件生成此程序集的引用。并且您必须在此文件中添加 using namespace System.Moles 和函数 [HostType("Moles")]

This is working like as wrapper to any assembly you want, for example mscorlib (this example baseing on Moles Assembly Wrapper of mscorlib). This giving to you power to replace any .NET method by the delegate written by coder.

This is not working automagicaly. You must first create before this start works, Moles XML configuration file with list of Assemblies to "Wrapper" and by this code Moles generate a References of this assembiles from config file. And you must in this file add using namespace System.Moles, and before function [HostType("Moles")]

云醉月微眠 2024-09-12 10:02:35

有一个题为“Moles:工具辅助环境隔离与闭包”的演示。请注意结尾“WITH CLOSURES”。该书写于 2010 年。作者之一是 Jonathan de Halleux。我假设这是 Peli de Halleux 的正式名字。本文可以在“对象、模型、组件、模式:第 48 届国际会议,TOOLS 2010,西班牙马拉加,2010 年 6 月 28 日至 7 月 2 日,会议记录”一书中找到。以下是文章示例页面的链接:

https://www.google.com/books/edition/Objects_Models_Components_Patterns/hvzmnCtNffUC?hl=en&gbpv=1&dq=moles+halleux&pg=PA253

这里是该书的链接:

https://www.google.com/books/edition/Objects_Models_Components_Patterns/hvzmnCtNffUC?hl=en&gbpv=1&dq=Objects,+Models,+Components&printsec=frontcover

《实用 Microsoft Visual Studio 2015》、《真实世界 .NET、C# 和 Silverlight》、《使用 Visual Studio 进行敏捷软件工程》、《Pro .NET 最佳实践》中提到了 Pex 和 Moles。请参阅“Pex 和 Moles - .NET 的隔离和白盒单元测试”:

http://research。 micvrosoft.com/projects/pex/

Archive.org 上这 3 个视频的最新 URL 为:

https://web.archive.org/web/20210917122427/http://channel9。 msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-13

https://web.archive.org/web/20210917131545/http://channel9 .msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-23

https://web.archive.org/web/20210917141034/http:// channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-33

Peli de Halleux 的更多视频:

https://web .archive.org/web/20210514205423/https://channel9.msdn.com/Shows/Going+Deep/Nikolai-Tillman-and-Peli-de-Halleux-Inside-Code-Digger

https://web.archive.org/web/20210624042526/https://channel9.msdn.com/Blogs/matthijs/Pex-Unit-Testing-of-SharePoint-Services-that-Rocks

There is a presentation titled "Moles: Tool-Assisted Environment Isolation With Closures". Note the ending, "WITH CLOSURES". It was written in 2010. One of the authors was Jonathan de Halleux. I will assume that this is the formal first name of Peli de Halleux. This paper can be found in the book "Objects, Models, Components, Patterns: 48th International Conference, TOOLS 2010, Málaga, Spain, June 28 - July 2, 2010, Proceedings". Here is a link to sample pages of the article :

https://www.google.com/books/edition/Objects_Models_Components_Patterns/hvzmnCtNffUC?hl=en&gbpv=1&dq=moles+halleux&pg=PA253

Here is a link to the book :

https://www.google.com/books/edition/Objects_Models_Components_Patterns/hvzmnCtNffUC?hl=en&gbpv=1&dq=Objects,+Models,+Components&printsec=frontcover

Pex and Moles are mentioned in "Practical Microsoft Visual Studio 2015", "Real World .NET, C#, and Silverlight", "Agile Software Engineering with Visual Studio", "Pro .NET Best Practices". See "Pex and Moles - Isolation and White box Unit Testing for .NET" :

http://research.micvrosoft.com/projects/pex/

The latest URLs on Archive.org for the 3 videos are :

https://web.archive.org/web/20210917122427/http://channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-13

https://web.archive.org/web/20210917131545/http://channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-23

https://web.archive.org/web/20210917141034/http://channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-33

More videos with Peli de Halleux :

https://web.archive.org/web/20210514205423/https://channel9.msdn.com/Shows/Going+Deep/Nikolai-Tillman-and-Peli-de-Halleux-Inside-Code-Digger

https://web.archive.org/web/20210624042526/https://channel9.msdn.com/Blogs/matthijs/Pex-Unit-Testing-of-SharePoint-Services-that-Rocks

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