Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9316437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:37:05+00:00 2026-06-19T02:37:05+00:00

I’m using latest MEF 2 preview from Codeplex homepage , and it’s supposed to

  • 0

I’m using latest MEF 2 preview from Codeplex homepage, and it’s supposed to add open-generics support. It does, but in this specific case, MEF fails to compose generic implementation of generic interface:

public interface IOuter
{
    IInner Value { get; }
}

[Export(typeof(IOuter))]
public class MyOuter : IOuter
{
    [ImportingConstructor]
    public MyOuter(InnerGenericClass<string, int> value)
    {
        this.Value = value;
    }

    public IInner Value { get; private set; }
}

public interface IInner
{
    void Say();
}
public interface IGenericInner<T, K> : IInner
{
    // something else here
}

[Export(typeof(IGenericInner<,>))]
public class InnerGenericClass<T, K> : IGenericInner<T, K>
{
    public void Say()
    {
        Console.WriteLine("{0}, {1}", typeof(T), typeof(K));
    }
}

class Startup
{
    public void CatalogSetup()
    {
        var catalog = new AggregateCatalog(
            new AssemblyCatalog(Assembly.GetExecutingAssembly())
            );
        var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);

        var batch = new CompositionBatch();

        container.Compose(batch);

        var outer = container.GetExportedValue<IOuter>();
        outer.Value.Say();
    }
}

Here’s CompositionExpection:

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No exports were found that match the constraint: 
    ContractName    ConsoleApplication1.InnerGenericClass(System.String,System.Int32)
    RequiredTypeIdentity    ConsoleApplication1.InnerGenericClass(System.String,System.Int32)

Resulting in: Cannot set import 'ConsoleApplication1.MyOuter.Value (ContractName="ConsoleApplication1.InnerGenericClass(System.String,System.Int32)")' on part 'ConsoleApplication1.MyOuter'.
Element: ConsoleApplication1.MyOuter.Value (ContractName="ConsoleApplication1.InnerGenericClass(System.String,System.Int32)") -->  ConsoleApplication1.MyOuter -->  AssemblyCatalog (Assembly="ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot get export 'ConsoleApplication1.MyOuter (ContractName="ConsoleApplication1.IOuter")' from part 'ConsoleApplication1.MyOuter'.
Element: ConsoleApplication1.MyOuter (ContractName="ConsoleApplication1.IOuter") -->  ConsoleApplication1.MyOuter -->  AssemblyCatalog (Assembly="ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

Same exception is thrown even when I move importing of InnerGenericClass to attribute of MyOuter.Value:

[Export(typeof(IOuter))]
public class MyOuter : IOuter
{
    [Import(typeof(InnerGenericClass<string, int>))]
    public IInner Value { get; private set; }
}

What is weird, is that, it does work, when I change import type to IGenericInner:

[Export(typeof(IOuter))]
public class MyOuter : IOuter
{
    [ImportingConstructor]
    public MyOuter(IGenericInner<string, int> value)
    {
        this.Value = value;
    }

    public IInner Value { get; private set; }
}

What is even weirder, is that, it does not work, when importing via attribute.

Summary: I can’t use generic interface to import object to Value property, because there could be more implementations of IGenericInner interface (and also I want to import a specific implementation, but that’s not important).

I hope I won’t have to bypass MEF altogether in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:37:07+00:00Added an answer on June 19, 2026 at 2:37 am

    Cause your exporting the interface IGenericInner<> but want to import the specific class InnerGenericClass MEF does not find the correct part. You can export and import the specific class or create two exports of InnerGenericClass<> this way:

    [Export(typeof(IGenericInner<,>))]
    [Export(typeof(InnerGenericClass<,>))]
    public class InnerGenericClass<T, K> : IGenericInner<T, K> {
        public void Say() {
            Console.WriteLine("{0}, {1}", typeof(T), typeof(K));
        }
    }
    

    I just tested this in a VS .NET 4.5 project and it works.
    Btw. MEF 2 is already released in .NET 4.5 – If possible I recommend using System.ComponentModel.Composition.dll part of the framework not the preview version.

    UPDATE:
    A third solution (which will work with the Preview Version 5) could be to use additionally a string contract name. I personally try to avoid this cause of the ugly syntax, but ok.
    Code will looks like this:

    [Export(typeof(IOuter))]
    public class MyOuter : IOuter {
        [ImportingConstructor]
        public MyOuter([Import("MySpecialInnerGenericClass", typeof(IGenericInner<,>))]InnerGenericClass<string, int> value) {
            this.Value = value;
        }
    
        public IInner Value { get; private set; }
    }
    [Export("MySpecialInnerGenericClass", typeof(IGenericInner<,>))]
    public class InnerGenericClass<T, K> : IGenericInner<T, K> {
        public void Say() {
            Console.WriteLine("{0}, {1}", typeof(T), typeof(K));
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
Does anyone know how can I replace this 2 symbol below from the string
I am using jsonparser to parse data and images obtained from json response. When
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.