【发布时间】:2013-07-04 03:56:01
【问题描述】:
我试图找到一个例子,但没有成功,所以这就是我问这个问题的原因。
让我们从一些代码开始。这是我的代码:
class Dummy
{
public void DoDummyThings1()
{
Console.WriteLine("Sorry, I'm dummy 1...");
}
public void DoDummyThings2()
{
Console.WriteLine("Sorry, I'm dummy 2...");
}
public void DoDummyThings3()
{
Console.WriteLine("Sorry, I'm dummy 3...");
}
}
还有我的测试代码:
[TestClass]
public class UnitTest
{
private Dummy dum = new Dummy();
[TestInitialize()]
public void SetUp()
{
MethodInfo mi = typeof (UnitTest).GetMethod("TestDummy");
MethodBody mb = mi.GetMethodBody();
}
[TestMethod]
public void TestDummy()
{
this.dum.DoDummyThings1();
this.dum.DoDummyThings2();
this.dum.DoDummyThings3();
}
}
这就是我想要做的。我想在执行每个测试方法之前查看测试方法并检查是否会调用 Dummy 类的方法 DoDummyThings1、DoDummyThings2 和 DoDummyThings3。
这样做的目的是,根据调用的 DoDummyThingsX 方法,我想在代码深处的某处注入不同的实现,以在运行时修改某些类的行为(将接口的注入实现交换为另一个)。
谁能解释我如何正确地做到这一点(使用最新版本的 Cecil 或其他 C# 版本)? 有没有办法在不使用 .dll 文件的情况下做到这一点? (目前,这是我想出如何做到这一点的唯一方法,但是,我无法使用硬编码的字符串作为“MyDllName.dll”和“MyNamespace.MyClassName”)
我已经知道的其他 stackoverflow 线程:
- Look if a method is called inside a method using reflection
- How to determine which methods are called in a method?
- Can I use reflection to inspect the code in a method?
谁能帮助我提供一个完整(但简单)的示例(如果可能的话)? 谢谢!
【问题讨论】:
标签: c# reflection mstest system.reflection mono.cecil