Note that there are some explanatory texts on larger screens.

plurals
  1. POIntercepting explicit interface implementations with Castle Dynamic Proxy
    text
    copied!<p>I am having trouble getting Castle Dynamic Proxy to intercept methods that are explicit interface implementations. I read here <a href="http://kozmic.pl/category/dynamicproxy/" rel="nofollow">http://kozmic.pl/category/dynamicproxy/</a> that it should be possible to do this. Here are my classes;</p> <pre><code>internal interface IDomainInterface { string DomainMethod(); } public class DomainClass : IDomainInterface { string IDomainInterface.DomainMethod() { return "not intercepted"; } } </code></pre> <p>Here is my interceptor class;</p> <pre><code>public class DomainClassInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { if (invocation.Method.Name == "DomainMethod") invocation.ReturnValue = "intercepted"; else invocation.Proceed(); } } </code></pre> <p>And here is my test (which fails);</p> <pre><code> [TestClass] public void can_intercept_explicit_interface_implementation() { // Create proxy var generator = new ProxyGenerator(); var interceptor = new DomainClassInterceptor(); var proxy = (IDomainInterface)generator.CreateClassProxy(typeof(DomainClass), interceptor); // Invoke proxy method var result = proxy.DomainMethod(); // Check method was intercepted -- fails Assert.AreEqual("intercepted", result); } </code></pre> <p>In addition to not being able to intercept the explicit interface implementation, it also seems that I am not receiving a notification of a non-proxyable member. Here is my proxy generation hook (which acts as a spy);</p> <pre><code>public class DomainClassProxyGenerationHook : IProxyGenerationHook { public int NonProxyableCount; public void MethodsInspected() {} public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo) { NonProxyableCount++; } public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) { return true; } } </code></pre> <p>Here is my test (which again fails);</p> <pre><code> [TestMethod] public void receive_notification_of_nonproxyable_explicit_interface_implementation() { // Create proxy with generation hook var hook = new DomainClassProxyGenerationHook(); var options = new ProxyGenerationOptions(hook); var generator = new ProxyGenerator(); var interceptor = new DomainClassInterceptor(); var proxy = (IDomainInterface)generator.CreateClassProxy(typeof(DomainClass), options, interceptor); // Check that non-proxyable member notification was received -- fails Assert.IsTrue(hook.NonProxyableCount &gt; 0); } </code></pre> <p>Has anyone had success in getting DP to intercept explicit interface implementations? If so, how?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload