简体   繁体   English

MEF中的出口仿制药

[英]export generics in MEF

I want to export a generic class to a generic interface via MEF. 我想通过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: 但是当我尝试导出IService<T> ,我收到此错误:

Attribute argument cannot use type parameters 属性参数不能使用类型参数

Can anybody guide me to do this please? 有人可以指导我这样做吗?

Try 尝试

[Export(typeof(IService<>))]

To get a generic type definition from the typeof operator, you omit type arguments. 要从typeof运算符获取泛型类型定义,请省略类型参数。 For types with more than one type parameter, use commas to indicate the "arity" of the type. 对于具有多个类型参数的类型,请使用逗号表示该类型的“arity”。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM