Babak Fakhriloo
Babak Fakhriloo

Reputation: 2126

using generic interfaces in MEF

I am developing an application which is MEF enabled. There is a core library project which works as a glue and implements :

 CompositionContainer cc = new CompositionContainer(catalog);

 cc.ComposeParts(this);

I have declared all [Import] parts in this core library such below:

 [Import(typeof(IHost))]
        // The imported host form
        public IHost Host
        { get; set; }

   [Import(typeof(ILightStudents<?>))]
   public ILightStudents<?> StudentsAPI  { get; set; }

There in so problem in implementing IHost or other interfaces in other library projects which has [export] attribute, but problem here is that I have declared ILightStudents like this:

public interface ILightStudents<T> where T:class
    {
        IEnumerable<T> Students();

        T GetStudent(long id);

    }

But as you saw in previous code, I put '?' mark in import part. As you know the purpose of generic methods is that you can implement them by which ever class or type you want. And here I want to implement ILightStudents in other library project with my proper type, but I cant leave [import] part without specifying the type.

Would you help me please ?

Edited:

I almost could solve the problem by using dynamic type binding.

Upvotes: 3

Views: 2619

Answers (2)

TomTom
TomTom

Reputation: 62093

Use either:

  • The code in MefContrib or
  • MEF 2 preview.

This feature was added in both - it will be included in .NET 4.5.

Upvotes: 2

blindmeis
blindmeis

Reputation: 22435

i copied the answer from another thread here a few days ago:

Try

[Export(typeof(IService<>))]

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>)

Upvotes: 2

Related Questions