Search code examples
c#genericsmef

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


Solution

  • 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/