zoukankan      html  css  js  c++  java
  • Unity The Type Matching Rule

    Unity提供了匹配多个Type的规则TypeMatchingRule类型。和TagAttributeMatchingRule的tagToMatch参数一样,TypeMatchingRule的参数typeName一样不支持通配符。看一个简单的示例:

     1 public class MyObject
     2 {
     3   public virtual Int32 DoWork(Int32 i, Char c)
     4   {
     5     return i;
     6   }
     7 
     8   public virtual void DoWork2(Int32 i, Char c)
     9   {
    10 
    11   }
    12 
    13   public virtual void DoWork3()
    14   {
    15 
    16   }
    17 }
    18 
    19 IUnityContainer unityContainer = new UnityContainer();
    20 
    21 unityContainer.LoadConfiguration();
    22 unityContainer.Configure<Interception>()
    23   .AddPolicy(“TypeMatchingRule”)
    24   .AddMatchingRule(new TypeMatchingRule(“MyObject”))
    25   .AddCallHandler<Log4NetHandler>();
    26 unityContainer.RegisterType<MyObject>(
    27   new Interceptor<VirtualMethodInterceptor>(),
    28   new InterceptionBehavior<PolicyInjectionBehavior>()
    29 );
    30 
    31 MyObject myObject = unityContainer.Resolve<MyObject>();
    32 
    33 myObject.DoWork(Int32.MaxValue, Char.MaxValue);
    34 myObject.DoWork2(Int32.MaxValue, Char.MaxValue);
    35 myObject.DoWork3();

    配置文件如下:

    <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=”Microsoft.Practices.Unity.Interception” />
      <assembly name=”UnityTest6″ />
    
      <namespace name=”System” />
      <namespace name=”System.Collections.Generic” />
      <namespace name=”Microsoft.Practices.Unity.InterceptionExtension” />
      <namespace name=”UnityTest6″ />
    
      <container>
        <extension type=”Interception” />
    
        <interception>
          <policy name=”TypePolicy”>
            <matchingRule name=”TypeMatchingRule” type=”TypeMatchingRule”>
              <constructor>
                <param name=”typeName” value=”MyObject” />
              </constructor>
            </matchingRule>
            <callHandler name=”Log4NetHandler” type=”Log4NetHandler” />
          </policy>
        </interception>
    
        <register type=”MyObject”>
          <interceptor type=”VirtualMethodInterceptor” />
          <interceptionBehavior type=”PolicyInjectionBehavior” />
        </register>
      </container>
    </unity>
  • 相关阅读:
    centos7使用yum时报错”Error: rpmdb open failed“
    shell脚本监控httpd进程,如果进程异常自动重启服务
    DNS域名服务系统(二)--基本dns域名解析/主从dns解析
    DNS域名服务系统(一)
    ** server can't find mail.extmail.org: SERVFAIL
    搭建邮件服务器postfix向mysql导入extmail.sql文件时报错 ERROR 1064 (42000) at line 50: You have an error in your SQL syntax; 'TYPE=MyISAM COMMENT='Ext/Webman
    Nginx实现https和跳转功能
    Nginx的反向代理和负载均衡
    Nginx基于域名的虚拟主机
    jquery select chosen禁用某一项option
  • 原文地址:https://www.cnblogs.com/junchu25/p/2633424.html
Copyright © 2011-2022 走看看