1

如何配置Unity 2.0策略注入以在配置文件中使用自定義屬性匹配規則?如何配置Unity 2.0策略注入以在配置文件中使用自定義屬性匹配規則?

我想要的是翻譯統一配置文件中的以下代碼片段。

myContainer.Configure<Interception>() 
     .AddPolicy("MyPolicy") 
     .AddMatchingRule<CustomAttributeMatchingRule> 
      (new InjectionConstructor(typeof(MyAttributeType), true)) 
     .AddCallHandler<MyCallHandler> 
      ("MyValidator", 
      new ContainerControlledLifetimeManager()); 

回答

-1

可以配置如下,[TypeName]需要根據您的組件能夠被正確配置。

......

<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/> 

<container> 
    <extension type="Interception"/> 
    <interception> 
    <policy name="MyPolicy"> 
     <matchingRule name="customAttribute" type="CustomAttributeMatchingRule"> 
     <constructor> 
      <param name="attributeType" type="[MyAttributeType]"/> 
      <param name="inherited" value="true"/> 
     </constructor> 
     </matchingRule> 
     <callHandler name="MyValidator" type="[MyCallHandler]"> 
     <lifetime type="transient"/> 
     </callHandler> 
    </policy> 
    </interception> 
</container> 

......

+0

屬性類型需要自定義屬性的類型對象。如何在配置中傳遞我的自定義屬性的Type對象?或者我是否實現了一個自定義TypeConverter來獲取我的自定義屬性的Type對象? – surajnaik 2012-01-11 09:44:12

+0

它應該是「[YourAttributeNameSpace]。[YourAttributeTypeName],[YourAttributeAssemblyName]」,已經有一個TypeConverter可以被.NET配置系統計算出來。 – 2012-01-11 09:55:08

+1

由於某種原因,它不適合我。它拋出InvalidOperation異常。 – surajnaik 2012-01-11 10:20:33