在MEF出口仿制药 [英] export generics in MEF
本文介绍了在MEF出口仿制药的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过MEF一个泛型类导出到一个通用的接口。我的对象是:
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;
}
但是,当我尝试导出 IService< T>
,我得到这个错误:
属性参数不能使用类型参数
Attribute argument cannot use type parameters
任何人都可以指导我做这件事吗?
Can anybody guide me to do this please?
推荐答案
尝试
[Export(typeof(IService<>))]
要获得从的typeof
运营商泛型类型定义,你忽略类型参数。对于类型的多于一种类型的参数,使用逗号来指示的类型的元数。例如:
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>)
这篇关于在MEF出口仿制药的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文