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 6255489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:22:29+00:00 2026-05-24T14:22:29+00:00

My question title sounds a little bit difficult – sorry. I’m new in MEF

  • 0

My question title sounds a little bit difficult – sorry. I’m new in MEF :-).

My scenario:

public class MainClass
{
  [ImportMany(typeof(ITest))]
  private List<ITest> Tests { get; set; }

  public MainClass()
  {
    Init();
  }

  private void Init()
  {
     DirectoryCatalog catalog = new DirectoryCatalog(@"./");
     CompositionContainer container = new CompositionContainer(catalog);
     container.ComposeParts(this);
  }
}



[Export("BirthdayJob")]
[Export(typeof(ITest))]
public partial class BirthdayTest : ITest
{
  [ImportingConstructor]
  public BirthdayUserControl(IParameter parameter)
  {
     InitializeComponent();
     this.Parameter = jobParameter;
  }

   public IParameter Parameter { get; set; }
}

[Export(typeof(IParameter))]
[Export("BirthdayParameter")]
public class BirthdayJobParameter : IParameter
{
   public override string ToString()
   {
       return "Birthday Remember";
   }
}


public interface IParameter : IMefInterface
{

}

public interface IMefInterface
{

}

In the generic list of test, I should have all possible ITest objects with the associated IParameter object. Unfortunately, there aren’t any items in the generic list.

Can you help? What did I do wrong?

Thanks in advance.

Regards, pro

//Edit

So I have a compilable Class for my problem :

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace ObjectContracts
{
    public class Program
    {
        static void Main(string[] args)
        {
            var container = new CompositionContainer(new TypeCatalog(typeof (IFoo), typeof (Bar), typeof(Foo)));
            var bar = container.GetExportedValue<Bar>();
            Console.WriteLine(bar.Foo.Message);
            Console.ReadLine();

        }

    }

    [InheritedExport]
    public interface IFoo
    {
        string Message { get; set; }
    }

    [Export]
    public class Bar
    {
        [ImportingConstructor]
        public Bar([Import("Foo")]IFoo foo)
        {
            this.Foo = foo;
        }

        public IFoo Foo { get; set;}
    }

    [Export("Foo")]
    public class Foo : IFoo
    {
        public Foo()
        {
            Message = ":-)";
        }
        public string Message { get; set; }
    }
}

What do I do wrong? Please help me 🙂

Regards, patrick

  • 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-05-24T14:22:30+00:00Added an answer on May 24, 2026 at 2:22 pm

    The point is that if you use a contract (in your case “BirthdayJob”) in your export, you need to specify that in your import as well. Like so:

    [Export("BirthdayJob",typeof(ITest))]
    public partial class BirthdayTest : ITest
    {
      // class definition
    }
    

    And your import:

    public class MainClass
    {
      [ImportMany("BirthdayJob",typeof(ITest))]
      private List<ITest> Tests { get; set; }
    
      // class definition
    }
    

    The great thing about contracts is that they allow you to group instances of certain objects of a specific type and filter out any unwanted instances of objects of a specific type.

    MEF is the coolest!

    [Export("PeopleLivingInEdmontonAlbertaCanada",typeof(IPerson))]
    public Person KevinParkinson { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all Sorry if my question title sounds stupid.... I have the following
The question title is a bit strange because I'm not exactly sure how to
Sorry for vague question title. I've got a table containing huge list of, say,
Sorry if the question title is confusing. Let me explain further. I am building
As the question title reads, assigning 2^31 to a signed and unsigned 32-bit integer
Although the question title appears a bit subjective I am sure there is not
the question title might be a bit confuse. but here is my case. i
Sorry about the weird question title, but I don't really know what to call
Antescript: I'm aware that there's a prior SO question whose title sounds like it
The title sounds a bit odd, but what I am trying to do is

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.