Unity的AdditionalInterfaces问题

在研究Unity的AdditionalInterfaces时候产生一个疑问,假设现有类型的函数名称和一个接口的函数名称相同,通过附加接口后创建的代理类型规范了这一组操作。下面是模拟的定义和调用:

 1 public class MyObject4
 2 {
 3   public virtual void DoWork()
 4   {
 5 
 6   }
 7 }
 8 
 9 public class MyObject5 : MyObject4, IWorkProvider
10 {
11   public override void DoWork()
12   {
13     base.DoWork();
14   }
15 }
16 
17 IWorkProvider workProvider = new MyObject5();
18 
19 workProvider.DoWork();

现在通过Intercept的NewInstanceWithAdditionalInterfaces函数为MyObject4的派生代理类型添加IWorkProvider接口。代码如下:

 1 Object myObject = Intercept.NewInstanceWithAdditionalInterfaces(
 2   typeof(MyObject4),
 3   new VirtualMethodInterceptor(),
 4   new[] { new MyInterceptionBehavior() },
 5   new[] { typeof(IWorkProvider) }
 6 );
 7 
 8 IWorkProvider workHost = (IWorkProvider)myObject;
 9 
10 workHost.DoWork();

调用DoWork时抛出NotImplementedException,Additional interfaces do not have an implementation。也就是说Unity为一个类型附加接口的时候使用的是显示实现而不是隐式实现,同时也没有判断当前类型中是否已经存在了相同声明的隐式实现。

 1 public class MyObject5 : MyObject4, IWorkProvider
 2 {
 3   public override void DoWork()
 4   {
 5     base.DoWork();
 6   }
 7 
 8   #region IWorkProvider Members
 9 
10   void IWorkProvider.DoWork()
11   {
12     throw new NotImplementedException();
13   }
14 
15   #endregion
16 }
posted @ 2012-08-10 11:43  junchu25  阅读(358)  评论(0编辑  收藏  举报