如何拦截对匹配方法的调用?

时间:2011-06-23 05:22:30

标签: unity-container

下面是我的配置,我想拦截对匹配方法的调用。我该怎么做才能添加匹配方法?

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

    <sectionExtension
   type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
                    Microsoft.Practices.Unity.Interception.Configuration" />

    <alias alias="IDAL" type="InterceptionBlockApplication.IDAL,InterceptionBlockApplication"/>
    <alias alias="DALTest" type="InterceptionBlockApplication.DALTest,InterceptionBlockApplication"/>

    <container name="DALTest">



      <extension type="Interception"/>

      <interception>
        <policy name="TestPolicy">
          <matchingRule name="Method Signature Matching Rule"  type="MemberNameMatchingRule">

            <method name="MethodA"/>
            <method name="MethodB">

            </method> I try to do that. But it will throw a exception that:

           [ Configuration is incorrect, the type Microsoft.Practices.Unity.InterceptionExtension.MemberNameMatchingRule does not have a method named MethodA that takes parameters named .] 

         What should I do?

          </matchingRule>
          <callHandler name="MyLogCallHandler" type="InterceptionBlockApplication.MyLogCallHandler, InterceptionBlockApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
          </callHandler>
        </policy>
      </interception>

      <register type="IDAL" mapTo="DALTest" name="DALTest">
        <interceptor isDefaultForType="false" type="VirtualMethodInterceptor"/>
      </register>




    </container>
  </unity>

任何帮助都将受到赞赏。

问候。

大卫

1 个答案:

答案 0 :(得分:1)

类MemberNameMatchingRule有一些构造函数。例如,对于单个方法:

<matchingRule name="rule1" type="MemberNameMatchingRule">
        <constructor>
          <param name="nameToMatch" >
            <value value="MethodA"/>
          </param>
        </constructor>
      </matchingRule>

您可能想要使用的其他构造函数:

public MemberNameMatchingRule(
IEnumerable<MatchingInfo> matches
)
public MemberNameMatchingRule(
IEnumerable<string> namesToMatch
)
public MemberNameMatchingRule(
string nameToMatch
)
public MemberNameMatchingRule(
IEnumerable<string> namesToMatch,
bool ignoreCase
)
public MemberNameMatchingRule(
string nameToMatch,
bool ignoreCase
)

如果您需要传递IEnumerable,您可以阅读并遵循以下文章: How to configure Unity to inject an array for IEnumerable

或使用通配符,例如:

<matchingRule name="rule2" type="MemberNameMatchingRule">
        <constructor>
          <param name="nameToMatch" >
            <value value="Method*"/>
          </param>
          <param name="ignoreCase" value="true"/>
        </constructor>
      </matchingRule>