성 동적 프록시가 명시 적 인터페이스 구현 방식을 가로 채는 데 문제가 있습니다. 나는 이것을 할 수 있어야한다고 여기에서 http://kozmic.pl/category/dynamicproxy/을 읽었다. 내 수업은 다음과 같습니다.성 동적 프록시로 명시 적 인터페이스 구현 차단
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);
}
명시 적 인터페이스 구현을 가로 채지 못하는 것 외에도 프록시가 아닌 구성원에 대한 알림을받지 못하는 것 같습니다. 다음은 프록시 생성 훅 (스파이로 작동)입니다.
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를 얻는 데 성공한 사람이 있습니까? 그렇다면 어떻게?
그래, 지금 내가 원한 정확하게하고있다. 감사. – knick