假设我有一个接口(interface) IFoo
interface IFoo
{
int Bar();
int Bar2();
void VBar();
//etc,
}
我可以创建一个包装器来接受任何 IFoo 对象并在实际调用之前/之后做一些事情吗?
例如当我做这样的事情时
IFoo wrappedFoo = new Wrapper<IFoo>(actualFooObject).Object;
wrappedFoo.Bar();
然后 wrapper.Bar() 方法实际上执行这样的事情
PreCall(); //some code that I can define in the wrapper
actualFooObject.Bar();
PostCall();
有没有简单干净的方法来做到这一点?
最佳答案
您可以使用 Code Contracts对于这种方法。查看 user manual 的 2.8 Interface Contracts
部分(PDF)。
关于c# - 我可以创建一个包装器来拦截对实现特定接口(interface)的对象的所有调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15892514/