• Unity The Property Matching Rule


    Unity提供了针对类型属性的拦截规则PropertyMatchingRule类型。它的属性名称匹配通配符规则和MemberNameMatchingRule等类型相同。它可以指定拦截getter、setter或者同时拦截。看一个简单的示例:

     1 public class MyObject2
     2 {
     3   public virtual String ID { get; set; }
     4 
     5   public virtual String Name { get; set; }
     6 }
     7 
     8 public sealed class Log4NetHandler : ICallHandler
     9 {
    10   #region ICallHandler Members
    11 
    12   public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    13   {
    14     Console.WriteLine(input.MethodBase.Name);
    15 
    16     return getNext()(input, getNext);
    17   }
    18 
    19   public Int32 Order { get; set; }
    20 
    21   #endregion
    22 }
    23 
    24 IUnityContainer unityContainer = new UnityContainer();
    25 
    26 unityContainer.LoadConfiguration();
    27 unityContainer.Configure<Interception>()
    28   .AddPolicy(“PropertyMatchingRule”)
    29   .AddMatchingRule(new PropertyMatchingRule(“Name”, PropertyMatchingOption.Set))
    30   .AddCallHandler<Log4NetHandler>();
    31 unityContainer.RegisterType<MyObject2>(
    32   new Interceptor<VirtualMethodInterceptor>(),
    33   new InterceptionBehavior<PolicyInjectionBehavior>()
    34 );
    35 
    36 MyObject2 myObject2 = unityContainer.Resolve<MyObject2>();
    37 
    38 myObject2.Name = “aaa”;
    39 
    40 Console.WriteLine(myObject2.Name);

    上面的示例只有调用Name属性的setter才会被拦截。配置文件定义如下:

    <unity xmlns=”http://schemas.microsoft.com/practices/2010/unity”>
      <sectionExtension type=”Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration” />
    
      <assembly name=”mscorlib, 2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ />
      <assembly name=”UnityTest6″ />
    
      <namespace name=”UnityTest6″ />
    
      <container>
        <extension type=”Interception” />
    
        <interception>
          <policy name=”PropertyPolicy”>
            <matchingRule name=”PropertyMatchingRule” type=”PropertyMatchingRule”>
              <constructor>
                <param name=”propertyName” value=”Name” />
                <param name=”option” value=”Set” />
              </constructor>
            </matchingRule>
            <callHandler name=”Log4NetHandler” type=”Log4NetHandler” />
          </policy>
        </interception>
    
        <register type=”MyObject2″>
          <interceptor type=”VirtualMethodInterceptor” />
          <interceptionBehavior type=”PolicyInjectionBehavior” />
        </register>
      </container>
    </unity>
  • 相关阅读:
    代码书写过程中的一些需要培养的好习惯(持续更新)
    arm linux 移植 PHP
    arm linux 支持 wifi (wpa_supplicant)
    arm linux 移植 OpenCV
    使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整
    视频编解码 基本概念:GOP
    arm linux 移植 python3.6
    读懂反向传播算法(bp算法)
    FFmpeg命令详解
    (转)浅谈 Linux 内核无线子系统
  • 原文地址:https://www.cnblogs.com/junchu25/p/2633400.html
Copyright © 2020-2023  润新知