2016-02-05 60 views
0

看点看起来像这样Postsharp - 介绍虚拟方法被调用,而不是覆盖方法

[Serializable] 
[IntroduceInterface(typeof(ISomeMethod), OverrideAction = InterfaceOverrideAction.Ignore)] 
public class MyAspect: InstanceLevelAspect, ISomeMethod 
{ 
    [IntroduceMember(IsVirtual = true, OverrideAction = MemberOverrideAction.Ignore)] 
    public string SomeMethod() 
    { 
     throw new NotImplementedException(); 
    } 

    [OnMethodInvokeAdvice, MulticastPointcut(Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public)] 
    public void OnInvoke(MethodInterceptionArgs args) 
    { 
     var something = args.Instance as ISomeMethod; 

     //here is problem 
     string str = something.SomeMethod(); 

     args.Proceed(); 
    } 
} 

,当我在dotPeek检查,是的someMethod引入是虚拟的。 Aspect应用于与不同项目中的子类相同的基类。问题是当我重写此方法并调用OnInvoke拦截器时,SomeMethod方面实际上是用NotImplementedException而不是overriden方法调用的。调试器确认我在args.Instance中拥有正确的实例。这怎么可能?谢谢你的回答。

+0

你的代码在哪里试图覆盖该方法看起来像什么? – Batavia

+0

更多代码http://support.sharpcrafters.com/discussions/problems/3039-introduced-virtual-method-called-instead-overriden – sanjuro

回答

1

这是一个在PostSharp将来要解决的问题自PostSharp 4.2.22/4.3.5开始解决。

原来答案的其余部分:

但有一种变通方法。

让我看到问题中描述的代码。在项目中的基础类是这样的:

[MyAspect] 
public abstract class BaseClass 
{ 
    public void InterceptedMethod(string message) 
    { 
    } 
} 

子类B工程重现这个Bug是这样的:

public class ChildClass : BaseClass 
{ 
    public override string SomeMethod() 
    { 
     return ""; 
    } 
} 

现在的解决方法是实现接口的方法,而不是重写它。

public class ChildClass : BaseClass, ISomeMethod 
{ 
    public string SomeMethod() 
    { 
     return ""; 
    } 
} 
+0

谢谢,我被固定到我的课程的设计和这个错误这么多,我没有'吨通知这种简单的解决方法,这将是足够的,我正在努力完成:) – sanjuro