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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:38:03+00:00 2026-05-30T00:38:03+00:00

Consider the following code sample that uses MEF to create an object of type

  • 0

Consider the following code sample that uses MEF to create an object of type Importer that imports an object of type ImporterExporter which in turn imports an object of type Exporter, i.e. Importer -> ImporterExporter -> Exporter. The catalog is managed by a CompositionUtility (obviously simplified for this example).

I know that MEF will resolve imports recursively on imported parts. However, because I want to have the option to instantiate each of these classes independetly, every class with imports also composes itself in its constructor to resolve those imports.

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

namespace MefRecursionSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // var importerExporter = new ImporterExporter(); // include this and composition will work
            var importer = new Importer();
            Console.Write(importer.ImporterExporter.Exporter.Value); // should print 7
            Console.ReadKey();
        }
    }

    class CompositionUtility
    {
        static CompositionUtility()
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            var assemblyCatalog = new AssemblyCatalog(executingAssembly);
            _compositionContainer = new CompositionContainer(assemblyCatalog);
        }

        private static CompositionContainer _compositionContainer;
        private static bool _isComposing;

        public static void Compose(object part)
        {
            _compositionContainer.ComposeParts(part);
        }
    }

    class Importer
    {
        public Importer()
        {
            CompositionUtility.Compose(this);
        }

        [Import]
        public ImporterExporter ImporterExporter { get; set; }
    }

    [Export]
    class ImporterExporter
    {
        public ImporterExporter()
        {
            CompositionUtility.Compose(this);
        }

        [Import]
        public Exporter Exporter { get; set; }
    }

    [Export]
    class Exporter
    {
        public int Value { get { return 7; } }
    }

}

Running the code as is leads to a composition error “The ComposablePart of type MefRecursionSample.Importer’ cannot be recomposed….”, obviously because I am trying to explictly compose something that MEF also wants to compose.

What surprised me, was the fact that when I included the first line of the Main method, i.e. create an object of type ImporterExporter without MEF, this “double-composition” no longer caused an exception. Why is that?

Also, how could I make it work such that I could instantiate each of these indepennetly, yet also make them compose themselves when chained as in the sample. I figured I would introduce a boolean flag _compositionInProgress on the CompositionUtility and immediately return from Compose() when the flag is set to avoid the recursive composition. Is there a better way?

  • 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-30T00:38:04+00:00Added an answer on May 30, 2026 at 12:38 am

    The flag I considered setting in the CompositionUtility‘s Compose method does not work, because there can be cases where the string of automatic imports is interrupted. For instance, in the question’s example if Exporter instantiated a class in its constructor using new and this class would want to compose itself. Under the original solution, that class’ call to Ccompose would return immediately, leaving the class uncomposed.

    Because I want classes to compose themselves (thus making it unneccessary for their users to even know about MEF), the only solution was to establish the rule that classes with an [Export] attribute must not call Compose(this). Because they will be composed automatically by MEF when being imported, this would result in “double composition” and thus throw an exception.

    If it is a requirement that classes marked with [Export] have to instantiated independently via new instead of nly imported via MEF, they have to have an addional constructor with a boolean flag which when set well trigger composition of that class. The default behavior, however, has to be no composition in order to avoid the aforementioned “double composition”.

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

Sidebar

Related Questions

Consider the following sample code which uses a TrustManager to log whether an outgoing
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code sample: interface IData { int Count(); } interface IOperations {
Consider the following sample code: class SampleClass { public long SomeProperty { get; set;
Consider the following sample code: class MyClass { public long x; public void DoWork()
Consider the following sample code. #define T(q) L##q #define A(p) T(x T(#p)) wchar_t w[]
Consider the following simple code to create a typesafe equals. This first section allows
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following sample code below: #include <iostream> using namespace std; class base {

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.