我有一個泛型類,它使用TBase作爲類型參數。使用MEF,我想要一個它應該導入的泛型類型列表。我試圖使用這樣的:MEF通用類與構造函數作爲類型合同
1)
[ImportMany(typeof(TBase))]
public List<TBase> ObjectList { get; set; }
2)
Type IValueType = typeof(TBase)
[ImportMany(IValueType)]
public List<TBase> ObjectList{ get; set; }
3)
[ImportMany(TBase)]
public List<TBase> ObjectList{ get; set; }
第一節目
{'TBase': an attribute argument cannot use type parameters
}
第二節目
{An object reference is required for the non-static field, method, or property
}
第三節目
{'TBase' is a 'type parameter' but is used like a 'variable'
}
我在這裏做錯了什麼?我如何解決它?
是的,如果TBase是一個泛型參數,它在編譯時是不知道的,我不相信它會讓你把這些信息嵌入到一個屬性中。然而,正如Igor指出的那樣,您可以簡單地將該值從ImportMany屬性關閉,這會導致MEF查看屬性的類型以獲取合同類型,在這種情況下,會爲對象實例構建任何類型的TBase。請記住,MEF目前不支持開放式泛型,因此爲了使其顯示在目錄中,必須使用封閉的泛型類型。 – 2010-06-04 16:12:52