首页 > 技术文章 > castle windsor学习----- Referencing types in XML 在xm文件中引用类型

lanpingwang 2017-03-11 00:55 原文

当从xml引用installer的语法如下

<install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Infrastructure"/>

Windsor允许你省略一部分命名规则。如

<install type="Acme.Crm.Infrastructure.ServicesInstaller"/>

甚至可以省略命名空间。如

<install type="ServicesInstaller"/>

当使用如上的简写语法时,Windsor将试图在应用程序域中的程序集(不包括BCL程序集)查找类型。如果你加载一些特殊的程序集(例如不在编辑期间进行依赖的程序集),

你可以在xml文件中配置如下节点就可以添加扫描程序集了

<using assembly="Acme.Crm.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11111111111111" />

 

你也可以使用.dll或.exe文件路径代替程序集名称

这个方式类似c#中的using用法,这里只是引用程序集代替了命名空间

如果没有唯一的类型被Windsor找到,则会引发异常,你将需要扩展一些信息保证类型的唯一性

 

Limitations

By default BCL types (defined in System.* assemblies or mscorlib) will not be matched when using shorthand syntax. Also arrays and generics containing BCL types will not be matched. You will have to use full type name then

Generics

Generic types are supported using following syntax:

For simple generic type IGeneric<ICustomer>:

IGeneric`1[[ICustomer]]

For generic type with multiple generic parameters like IDoubleGeneric<ICustomer, ISpecification>:

IDoubleGeneric`2[[ICustomer],[Acme.Crm.ISpecification]]

Notice you can mix and match levels of details provided, specifying some types by just name, while other by namespace and name.

For nested generic type with multiple generic parameters like IDoubleGeneric<ICustomer, IDoubleGeneric<ICustomer, IClock>>:

IDoubleGeneric`2[[ICustomer],[IDoubleGeneric`2[[ICustomer],[IClock]]]]

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!--<using assembly="WindsorInstaller"/>-->
  <installers>
    <install type="CustomerInstaller"/>
  </installers>
  <!--<components>
    <component type="Service.Log,Service" service="IService.ILog,IService">
      
    </component>
  </components>-->
</configuration>

如上配置install,如果type的值只是类型的名称,如果该类型不在当前的程序集中,则需要using引入该类型所在的程序集才能正常解析

推荐阅读