MEF Generic Class With Constructor as Type Contract
I have a generics class, that uses TBase as the type parameter. Using MEF, I wanted a list of Generic Type that it should Import. I tried to use this :
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; }
The first Shows
{'TBase': an attribute argument cannot use type parameters
}
The second Shows
{An object reference is required for the non-static field, method, or property
}
The third Shows
{'TBase' is a 'type parameter' but is used like a 'variable'
}
What am I Doing wrong her开发者_如何学Ce? How can I Fix it?
Try the following syntax:
[ImportMany]
public IEnumerable<TBase> ObjectList{ get; set; }
EDIT The first syntax should work as [ImportMany(typeof(TBase))]
is a legal statement and ImportMany
does take a type in of its constructors/
MEF requires arguments that can be converted to constant strings at compile time. Since you are using TBase that is generic and can be realized only at runtime, MEF metadata can't be produced. Try using a non generic interface instead of the generic type TBASE.
精彩评论