c# - 我可以创建一个包装器来拦截对实现特定接口(interface)的对象的所有调用吗?

标签 c# .net methods interface

假设我有一个接口(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 manual2.8 Interface Contracts 部分(PDF)。

关于c# - 我可以创建一个包装器来拦截对实现特定接口(interface)的对象的所有调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15892514/

相关文章:

c# - 如何创建一个使用模式如锁的方法?

c# - 无法在 MonoTouch 中的 NavigationItem.RightBarButtonItems 中放置两个按钮

c# - 在 C# 中从模型自动生成好友类

c# - 链接到设备管理器

javascript - 使用 Web API 的客户端 Web 应用程序,如何根据服务器端 Web API 的期望填充选择框字段值?

c++ - OpenCV:System.Runtime.InteropServices.SEHException

java - 访问泛型类的静态方法

c# - CaSTLe Windsor 处置命令

.net - DataWedge 库 - 摩托罗拉 MC55 条码扫描速度非常慢

C# 属性和方法