English 中文(简体)
与城堡动态代理拦截明确的接口实施
原标题:Intercepting explicit interface implementations with Castle Dynamic Proxy

I am having trouble getting Castle Dynamic Proxy to intercept methods that are explicit interface implementations. I read here http://kozmic.pl/category/dynamicproxy/ that it should be possible to do this. Here are my classes;

internal interface IDomainInterface
{
    string DomainMethod();
}

public class DomainClass : IDomainInterface
{
    string IDomainInterface.DomainMethod()
    {
        return "not intercepted";
    }
}

这是我的拦截器课;

public class DomainClassInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        if (invocation.Method.Name == "DomainMethod")
            invocation.ReturnValue = "intercepted";
        else
            invocation.Proceed();
    }
}

这是我的考验。

    [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);
    }

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);

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;
    }
}

这是我的考验,我再次失败了。

    [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 > 0);
    }

有没有人成功地让DP拦截明确的接口执行?如果成功,如何成功?

最佳回答

您正在创建一个类代理。 类代理只拦截类中的虚拟方法, 而明确实施 C# 中的界面方法并不是虚拟的( 因为它是私人的 ) 。

如果您想要在接口上截取方法, 您需要明确告知动态Proxy

var proxy = (IDomainInterface)generator.CreateClassProxy(typeof(DomainClass), new Type[] { typeof(IDomainInterface) }, interceptor);

您的界面也标记为 internal , 以确保 DiveProxy 的 public (或公开接口,或添加 Internals VisibleTotrimitte ) 。

随着你的第一次测试的通过, 方法将被拦截。

问题回答

暂无回答




相关问题
castle IOC - resolving circular references

quick question for my MVP implementation: currently I have the code below, in which both the presenter and view are resolved via the container. Then the presenter calls View.Init to pass himself to ...

Castle Windsor auto registration

I currently have the following registration set up private static void AddFrameworkComponentsTo(IWindsorContainer container) { container.AddComponent<ITypeConverter, TypeConversionFacade>();...

Authorization and Windsor

I m trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute { private readonly IUserService _userService; public MyCustomAuth(IUserService ...

How can I override a component registered in Castle Windsor?

I m just starting with Windsor, so please be gentle :) I have a scenario where I want to be able to override/replace components placed inside a windsor container. Read on ... In my prod code, I want ...

热门标签