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

  • Home
  • SEARCH
  • 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 3675024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:55:31+00:00 2026-05-19T02:55:31+00:00

I am using MEF as DI container and the problem is that I want

  • 0

I am using MEF as DI container and the problem is that I want to import specific part from multiple parts.

For example, I have following codes :

public interface IService
{
    void Send();
}

[Export(typeof(IService))]
public class Service : IService
{
    public void Send()
    {
        Console.WriteLine("Service.Send");
    }
}

[Export(typeof(IService))]
public class FakeService : IService
{
    public void Send()
    {
        Console.WriteLine("FakeService.Send");
    }
}

[Import]
public IService Service { get; set; } // ---> let's say I want to use FakeService

Is there any solution?

Thanks in advance

  • 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-19T02:55:32+00:00Added an answer on May 19, 2026 at 2:55 am

    You can export metadata with your class, here’s an example:

    public interface ILogger
    {
      void Log(string message);
    }
    
    [Export(typeof(ILogger)), ExportMetadata("Name", "Console")]
    public class ConsoleLogger : ILogger
    {
      public void Log(string message)
      {
        Console.WriteLine(message);
      }
    }
    
    [Export(typeof(ILogger)), ExportMetadata("Name", "Debug")]
    public class DebugLogger : ILogger
    {
      public void Log(string message)
      {
        Debug.Print(message);
      }
    }
    

    Given that contract and those example implementations, we can import out types as Lazy<T, TMetadata> whereby we can define a metadata contract:

    public interface INamedMetadata
    {
      string Name { get; }
    }
    

    You don’t need to worry about creating an implementation of the metadata, as MEF will project any ExportMetadata attribute values as concrete implementation of TMetadata, which in our example is INamedMetadata. With the above, I can create the following example:

    public class Logger
    {
      [ImportMany]
      public IEnumerable<Lazy<ILogger, INamedMetadata>> Loggers { get; set; }
    
      public void Log(string name, string message)
      {
        var logger = GetLogger(name);
        if (logger == null)
          throw new ArgumentException("No logger exists with name = " + name);
    
        logger.Log(message);
      }
    
      private ILogger GetLogger(string name)
      {
        return Loggers
          .Where(l => l.Metadata.Name.Equals(name))
          .Select(l => l.Value)
          .FirstOrDefault();
      }
    }
    

    In that sample class, I am importing many instances, as Lazy<ILogger, INamedMetadata> instances. Using Lazy<T,TMetadata> allows us to access the metadata before accessing the value. In the example above, I’m using the name argument to select the appropriate logger to use.

    If it is not right to instantiate the class on import, you can use an ExportFactory<T,TMetadata> which allows you to spin up instances of your types on demand. (ExportFactory is included in the Silverlight version of .NET 4.0, but Glenn Block did throw the source code on codeplex for Desktop/Web use.

    I hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MEF I want to do the following. I have a WPF Shell. To
I have the following example code using MEF: public interface IFoo<T> {} public class
I have just started using MEF and have hit on an early problem. I
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I have been using the Windsor IoC Container for my web-based application, to resolve
I'm doing a proof of concept app in SL4 using MEF and as part
I am trying to get MEF to recompose all the parts that it knows
I have a MEF based solution that has several exported implementations of an interface.
My application is using MEF to export some classes from an external assembly. These
i have some simple Export/Import Scenario that i can't figur out why this not

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.