export generics in MEF

﹥>﹥吖頭↗ 提交于 2019-12-03 16:27:00

问题


I want to export a generic class to a generic interface via MEF. My objects are:

public interface IService<T> { }

[Export(typeof(IService<T>))] // error!!!!!!
public class Service<T> { }

public class Client<T> {
    [Import]
    private IService<T> _service;
}

But when I try to export IService<T>, I get this error:

Attribute argument cannot use type parameters

Can anybody guide me to do this please?


回答1:


Try

[Export(typeof(IService<>))]

To get a generic type definition from the typeof operator, you omit type arguments. For types with more than one type parameter, use commas to indicate the "arity" of the type. For example:

typeof(List<>)              // not: typeof(List<T>)
typeof(IDictionary<,>)      // not: typeof(IDictionary<K, V>)


来源:https://stackoverflow.com/questions/9295310/export-generics-in-mef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!