TypeMatchingRule XML配置解析类类型,而不是实现的接口

本文关键字:实现 接口 类型 配置 XML TypeMatchingRule | 更新日期: 2023-09-27 18:05:45

我正在根据自己的需要调整找到http://www.christophdebaene.com/blog/2008/11/02/aop-in-action-part-1-dirty-tracking-using-unity-interception/的代码

我想翻译我的工作Unity流畅API适应代码部分从Unity到企业库策略XML配置

从这个工作流畅的API…

   .AddNewExtension<Interception>()
   .RegisterInstance<ICallHandler>("DirtyCallHandler", new DirtyCallHandler(null))
   .Configure<Interception>()
            .AddPolicy("DirtyPolicy")
            .AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set))
            .AddMatchingRule(new CustomAttributeMatchingRule(typeof(DirtyAttribute), true))
            .AddMatchingRule(new InstanceOfMatchingRule(typeof(IDirty)))
            .AddCallHandler("DirtyCallHandler")

到这个XML配置…

 <policies>
    <add name="DirtyPolicy">
      <matchingRules>
        <add name="SetPropertyRule" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.PropertyMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection">
          <matches>
            <add matchOption="Set" match="*" />
          </matches>
        </add>
        <add name="DirtyAttributeRule" attributeType="IoCnAOP.DirtyAttribute, IoCnAOP" searchInheritanceChain="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.CustomAttributeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection"/>
        <add name="InstanceOfIDirtyRule" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TypeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection">
          <matches>
            <add match=" IoCnAOP.IDirty" ignoreCase="false"/>
          </matches>
        </add>
      </matchingRules>
      <handlers>
        <add name="DirtyCallHandler" order="0" type="IoCnAOP.DirtyCallHandler, IoCnAOP" />
      </handlers>
    </add>
  </policies>

问题位于match="IoCnAOP。如果类实现了接口idrty,则不匹配的idrty "(但是,如果我将其更改为以下match="Domain。"青蛙",我的意思是使用类类型,然后它的工作,但这不是我正在寻找的要求。我想要匹配类是否实现了给定的接口(id)

所以…如果我适应流畅的API代码工作得很好…但是XML配置只解析类类型…XML配置中缺少一些内容

我怎么能让它工作使用XML代替?

Thanks much in advance

p>
下面是一些相关的代码摘录…)
    public interface IDirty
    {
        bool IsDirty { get; set; }
    }
    public class Frog :  IDirty
    {
        public bool IsDirty { get; set; }
        public virtual string JustAProperty { get; [Dirty]set; }
    }
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class DirtyAttribute : Attribute
    {
    }

       public class InstanceOfMatchingRule : IMatchingRule
        {
            private readonly Type _type;
            public InstanceOfMatchingRule(Type type)
            {
                _type = type;
            }
            public bool Matches(System.Reflection.MethodBase member)
            {
                return _type.IsAssignableFrom(member.DeclaringType);
            }
        }
[ConfigurationElementType(typeof(CustomCallHandlerData))]
public class DirtyCallHandler : ICallHandler
{
    public DirtyCallHandler(NameValueCollection attributes)
    {
    }
    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        if (getNext == null)
            throw new ArgumentNullException("getNext");
        Console.WriteLine("DirtyCallHandle Invoking {0} at {1}", input.MethodBase.Name, DateTime.Now.ToLongTimeString());
        IsDirty(input.Target, input.MethodBase, input.Arguments[0]);
        return getNext()(input, getNext);
    }
    public int Order { get; set; }
    private static void IsDirty(object target, MethodBase methodBase, object value)  //http://www.christophdebaene.com/blog/2008/11/02/aop-in-action-part-1-dirty-tracking-using-unity-interception/
    {
        if (((IDirty)target).IsDirty)
            return;
        var propertyName = methodBase.Name.Substring("set_".Length);
        var info = target.GetType().GetProperty(propertyName);
        if (info == null)
            return;
        var oldValue = info.GetValue(target, null);
        if (!IsEqual(value, oldValue))
            ((IDirty)target).IsDirty = true;
    }
    private static bool IsEqual(object valueX, object valueY)
    {
        if (valueX == null && valueY != null)
            return false;
        if (valueX != null && valueY == null)
            return false;
        return valueX == null || valueX.Equals(valueY);
    }
}
    <?xml version="1.0" encoding="utf-8" ?>
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
      <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
      <container>
        <register type="Domain.Frog, Domain">
          <interceptor type="VirtualMethodInterceptor" />
          <policyInjection />
        </register>
      </container>
    </unity>

TypeMatchingRule XML配置解析类类型,而不是实现的接口

配置使用直接匹配类型和的Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TypeMatchingRule,而不是您的自定义InstanceOfMatchingRule

通过XML配置暴露InstanceOfMatchingRule的最简单方法是使用内置的CustomMatchingRuleData:
    [ConfigurationElementType(typeof(CustomMatchingRuleData))]
    public class InstanceOfMatchingRule : IMatchingRule
    {
        private readonly Type _type;
        public InstanceOfMatchingRule(NameValueCollection configuration)
        {
            _type = Type.GetType(configuration["targetType"]);
        }
        public InstanceOfMatchingRule(Type targetType)
        {
            _type = targetType;
        }
        public bool Matches(System.Reflection.MethodBase member)
        {
            return _type.IsAssignableFrom(member.DeclaringType);
        }
    }

然后使用配置:

<policyInjection>
    <policies>
        <add name="DirtyPolicy">
            <matchingRules>
                <add targetType="IoCnAOP.IDirty" type="IoCnAOP.InstanceOfMatchingRule, IoCnAOP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                    name="InstanceOfIDirtyRule" />
            </matchingRules>
            <handlers>
                <add name="DirtyCallHandler" order="0" type="IoCnAOP.DirtyCallHandler, IoCnAOP" />
            </handlers>
        </add>
    </policies>
</policyInjection>

通过NameValueCollection传递目标类型。对于编程使用,请使用Type构造函数。