age*_*t47 13 c# generics dependency-injection mef c#-4.0
我想通过MEF将泛型类导出到通用接口.我的目标是:
public interface IService<T> { }
[Export(typeof(IService<T>))] // error!!!!!!
public class Service<T> { }
public class Client<T> {
[Import]
private IService<T> _service;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试导出时IService<T>
,我收到此错误:
属性参数不能使用类型参数
有人可以指导我这样做吗?
pho*_*oog 25
尝试
[Export(typeof(IService<>))]
Run Code Online (Sandbox Code Playgroud)
要从typeof
运算符获取泛型类型定义,请省略类型参数.对于具有多个类型参数的类型,请使用逗号表示该类型的"arity".例如:
typeof(List<>) // not: typeof(List<T>)
typeof(IDictionary<,>) // not: typeof(IDictionary<K, V>)
Run Code Online (Sandbox Code Playgroud)