views:

134

answers:

1

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 here? How can I Fix it?

+1  A: 

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/

Igor Zevaka
Yes if TBase is a generic argument it isn't known at compile time and I don't believe it will let you embed that information into an attribute. However as Igor points out you can simply leave the value off the ImportMany attribute which will cause MEF to look at the type of the property to get the contract type, in this case whatever type is build to TBase for the object instances. Keep in mind however that MEF does not currently support open generics so in order for this to show up in the catalog it will have to be on a closed generic type.
Wes Haggard