Moles Isolation框架是如何实现的?
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。任何人都可以阐明吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Moles 实现了一个 CLR 探查器(特别是 ICorProfilerCallback 接口),该接口允许在 MSIL 方法体被 .NET 运行时编译为汇编代码之前重写它们。这是通过 JitCompileStarted 回调完成的。
在每个方法中,Moles 引入了一个如下所示的绕道:
当您设置摩尔时,您的委托将存储在底层 __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:
When you set a mole, your delegate is stored in the underlying __Detours dictionary which gets looked up whenver the method is executed.
这就像您想要的任何程序集的包装器一样,例如
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 ofmscorlib
). 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")]
有一个题为“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