Postsharp - 引入虚拟方法而不是覆盖方法

时间:2016-02-05 12:31:39

标签: c# postsharp

Aspect看起来像这样

[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中有正确的实例。这怎么可能?谢谢你的回答。

1 个答案:

答案 0 :(得分:1)

自PostSharp 4.2.22 / 4.3.5以来,这是一个在未来版本的PostSharp 中解决的的错误。

原始答案的其余部分:

但是有一种解决方法。

让我想象一下问题中描述的代码。项目A中的基类如下所示:

! com.sybase.jdbc4.jdbc.SybSQLException: There is already another cursor with the name 'jconnect_implicit_134' at the nesting level '0'.

! 

! at com.sybase.jdbc4.tds.Tds.processEed(Tds.java:4117) ~[jconn4-4.0.jar:JDK 1.6/jdbcmain/Thu Apr 30 04:18:00 PDT 2015]

! at com.sybase.jdbc4.tds.Tds.nextResult(Tds.java:3207) ~[jconn4-4.0.jar:JDK 1.6/jdbcmain/Thu Apr 30 04:18:00 PDT 2015]

! at com.sybase.jdbc4.tds.Tds.getResultSetResult(Tds.java:3973) ~[jconn4-4.0.jar:JDK 1.6/jdbcmain/Thu Apr 30 04:18:00 PDT 2015]

! at com.sybase.jdbc4.tds.TdsCursor.open(TdsCursor.java:328) ~[jconn4-4.0.jar:JDK 1.6/jdbcmain/Thu Apr 30 04:18:00 PDT 2015]

! at com.sybase.jdbc4.jdbc.SybStatement.executeQuery(SybStatement.java:2604) ~[jconn4-4.0.jar:JDK 1.6/jdbcmain/Thu Apr 30 04:18:00 PDT 2015]

! at com.sybase.jdbc4.jdbc.SybStatement.executeQuery(SybStatement.java:555) ~[jconn4-4.0.jar:JDK 1.6/jdbcmain/Thu Apr 30 04:18:00 PDT 2015]

! at org.eobjects.metamodel.jdbc.JdbcDataContext.execute(JdbcDataContext.java:381) ~[MetaModel-jdbc-3.4.7.jar:na]

! at org.eobjects.metamodel.jdbc.JdbcDataContext.executeQuery(JdbcDataContext.java:438) ~[MetaModel-jdbc-3.4.7.jar:na]

! Causing: org.eobjects.metamodel.MetaModelException: Could not execute query: There is already another cursor with the name 'jconnect_implicit_134' at the nesting level '0'.

! 

! at org.eobjects.metamodel.jdbc.JdbcUtils.wrapException(JdbcUtils.java:70) ~[MetaModel-jdbc-3.4.7.jar:na]

! at org.eobjects.metamodel.jdbc.JdbcDataContext.executeQuery(JdbcDataContext.java:444) ~[MetaModel-jdbc-3.4.7.jar:na]

项目B中重现错误的子类看起来像这样:

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

现在的解决方法是实现接口方法而不是覆盖它。

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