title: MEF inject container
body: <p>I`m building a simple app using MEF to better understand it, and I'm facing a problem. The app is a simple calculator that you can create new operations. Each operation is a class that exports IOperation. Here's the ADD operation class:</p>
  
  <pre><code>[Export(typeof(IOperation))]
  internal class Add : IOperation
  {
      CompositionContainer _container;
  
      public string Name
      {
          get { return "Add"; }
      }
  
      public string Symbol
      {
          get { return "+"; }
      }
  
      public IOperand Compute(params IOperand[] operands)
      {
          IOperand result = _container.GetExportedValue&lt;IOperand&gt;();
          result.Value = operands.Sum(e =&gt; e.Value);
          return result;
      }
  }
  </code></pre>
  
  <p>(IOperand is a interface that only exposes a double. The reason for it is that in version 2 you can have a expression like "(2+2)*4")</p>
  
  <p>My problem as you can see is that <code>_container</code> is null when I hit <code>Compute</code>. This concrete class is created when the container composes a <code>[ImportMany(typeof(IOperation))]</code>. So my question is: Is there a way to tell the container who is inverting control to pass a reference of himself to this object?</p>
  
  <p>PS:I would not like to make <code>_container</code> a public property.</p>
  
  <p><strong>Edit1:</strong> Here is the only implementation of IOperand so far:</p>
  
  <pre><code>[Export(typeof(IOperand))]
  public class SimpleResult : IOperand
  {
      private double _value;
      public double Value
      {
          get
          {
              return _value;
          }
          set
          {
              _value = value;
          }
      }
  }
  </code></pre>
  
  <p>This is the "Main", where composition happens:</p>
  
  <pre><code>public class Calculator
  {
      [ImportMany(typeof(IOperation))]
      private List&lt;IOperation&gt; _knownOperations;
      private List&lt;ICalculatorButton&gt; _buttons;
      private CompositionContainer _container;
  
      public Calculator()
      {
          _container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
          _container.SatisfyImportsOnce(this);
          _buttons = new List&lt;ICalculatorButton&gt;();
  
          ICalculatorButton button;
          for (int i = 0; i &lt; 10; i++)
          {
              button = _container.GetExportedValue&lt;IDigitButton&gt;();
              button.Symbol = i.ToString();
              ((IDigitButton)button).Value = i;
              _buttons.Add(button);
          }
          foreach (IOperation op in _knownOperations)
          {
              button = _container.GetExportedValue&lt;IOperationButton&gt;();
              button.Symbol = op.Symbol;
              ((IOperationButton)button).Operation = op;
              _buttons.Add(button);
          }
      }
  
      public IReadOnlyList&lt;IOperation&gt; KnownOperations
      {
          get { return _knownOperations.AsReadOnly(); }
      }
  
      public IReadOnlyList&lt;ICalculatorButton&gt; Buttons
      {
          get { return _buttons.AsReadOnly(); }
      }
  
      public IOperand Calculate(IOperation operation, params IOperand[] operands)
      {
          IOperand result = operation.Compute(operands);
          return result;
      }
  
      public IOperand Calculate(IOperation operation, params double[] operands)
      {
          List&lt;IOperand&gt; res = new List&lt;IOperand&gt;();
          foreach (double item in operands)
          {
              IOperand aux = _container.GetExportedValue&lt;IOperand&gt;();
              aux.Value = item;
              res.Add(aux);
          }
          return Calculate(operation, res.ToArray());
      }
  }
  </code></pre>