2010-04-28 25 views
3

我有以下的一般壽命經理與一般壽命管理工作,團結配置部分

public class RequestLifetimeManager<T> : LifetimeManager, IDisposable 
    { 
    public override object GetValue() 
    { 
     return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName]; 
    } 
    public override void RemoveValue() 
    { 
     HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName); 
    } 
    public override void SetValue(object newValue) 
    { 
     HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue; 
    } 
    public void Dispose() 
    { 
     RemoveValue(); 
    } 
} 

如何在統一配置部分引用此。創建一個類型別名

<typeAlias alias="requestLifeTimeManager`1" type=" UI.Common.Unity.RequestLifetimeManager`1, UI.Common" /> 

並指定其作爲一生的經理

<types> 
    <type type="[interface]" mapTo="[concretetype]" > 
     <lifetime type="requestLifeTimeManager`1" /> 
    </type> 
    </types> 

導致以下錯誤

Cannot create an instance of UI.Common.Unity.RequestLifetimeManager`1[T] because Type.ContainsGenericParameters is true. 

你如何引用泛型一輩子經理人?

回答

4

在引用泛型類型時不能使用類型別名,因此必須顯式引用類型。下面現在的作品

<container name="defaultContainer"> 
    <types> 
    <type type="ILayoutManager" mapTo="LayoutManager" > 
     <lifetime type="Publishing.UI.Common.Unity.RequestLifetimeManager`1[[Publishing.BLL.Managers.LayoutManager, Publishing.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02010e20f60e4d2]], Publishing.UI.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02010e20f60e4d2" /> 
    </type> 
    </types> 
</container>