• Unity The Assembly Matching Rule


    Unity的拦截策略中提供了程序集匹配规则,我们可以对于属于某个特定程序集的类型附加CallHandler。看一个简单的示例:

     1 public class MyObject
     2 {
     3   public virtual void DoWork()
     4   {
     5 
     6   }
     7 }
     8 
     9 public class MyCallHandler : ICallHandler
    10 {
    11   #region ICallHandler Members
    12 
    13   public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    14   {
    15     return getNext()(input, getNext);
    16   }
    17 
    18   public Int32 Order { get; set; }
    19 
    20   #endregion
    21 }
    22 
    23 public class MyCallHandler2 : ICallHandler
    24 {
    25   #region ICallHandler Members
    26 
    27   public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    28   {
    29     return getNext()(input, getNext);
    30   }
    31 
    32   public Int32 Order { get; set; }
    33 
    34   #endregion
    35 }
    36 
    37 IUnityContainer unityContainer = new UnityContainer();
    38 
    39 unityContainer.LoadConfiguration();
    40 unityContainer.Configure<Interception>()
    41   .AddPolicy(“AssemblyPolicy”)
    42   .AddMatchingRule(new AssemblyMatchingRule(Assembly.GetEntryAssembly()))
    43   .AddCallHandler<MyCallHandler>()
    44   .AddCallHandler<MyCallHandler2>();
    45 
    46 unityContainer.RegisterType<MyObject>(
    47   new Interceptor<VirtualMethodInterceptor>(),
    48   new AdditionalInterface<INotifyPropertyChanged>(),
    49   new InterceptionBehavior<PolicyInjectionBehavior>()
    50 );
    51 
    52 MyObject myObject = unityContainer.Resolve<MyObject>();
    53 
    54 myObject.DoWork();

    AssemblyMatchingRule的构造函数允许传递字符或者Assembly信息。上面的示例传递了当前AppDomain入口的EntryAssembly,MyObject类型属于这个Assembly中,所以MyCallHandler和MyCallHandler2在DoWork函数调用时都会被依次调用。同样上面的代码也可以通过配置文件定义:

    <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=”System.ComponentModel” />
      <namespace name=”UnityTest6″ />
    
      <container>
        <extension type=”Interception” />
    
        <interception>
          <policy name=”AssemblyPolicy”>
            <matchingRule name=”AssemblyMatchingRule” type=”AssemblyMatchingRule”>
              <constructor>
                <param name=”assemblyName” value=”UnityTest6″ />
              </constructor>
            </matchingRule>
            <callHandler name=”MyCallHandler” type=”MyCallHandler” />
          </policy>
        </interception>
    
        <register type=”MyObject”>
          <interceptor type=”VirtualMethodInterceptor” />
          <addInterface type=”INotifyPropertyChanged” />
          <interceptionBehavior type=”PolicyInjectionBehavior” />
        </register>
      </container>
    </unity>
  • 相关阅读:
    adb
    Android 入门第一课 一个简单的提示框
    JAVA基础入门
    Android环境配置
    Redis_基本类型介绍和指令___3
    Redis_基本类型介绍和指令___2
    Redis_基本类型介绍和指令___1
    Rediss_基本介绍
    int 指令
    关于学习汇编的一些规则的理解(div mul cf of)
  • 原文地址:https://www.cnblogs.com/junchu25/p/2631602.html
Copyright © 2020-2023  润新知