How to specify which part is imported

May 10, 2010 at 4:05 AM

Hi Everyone, I'm investigating MEF for a new project - it looks promising, but I'm a little stuck.

What I am trying to do, is create some parts that (in this case) return numbers, these parts can have parameters that are other numeric parts.

I have the following code...

    interface INumber
    { double Value { get; } }

    [Export(typeof(INumber))]
    class Number : INumber
    {
        public double Value
        { get; set; }
    }

    [Export(typeof(INumber))]
    class Addition : INumber
    {
        public double Value
        { get {return Parameter1.Value + Parameter2.Value;}}

        [Import(typeof(INumber))]
        INumber Parameter1;

        [Import(typeof(INumber))]
        INumber Parameter2;
    }

    [Export(typeof(INumber))]
    class Subtraction : INumber
    {
        public double Value
        { get { return Parameter1.Value - Parameter2.Value; }}

        [Import(typeof(INumber))]
        INumber Parameter1;

        [Import(typeof(INumber))]
        INumber Parameter2;
    }

I would like to be able to "wire-up" the parts using a xml config file, something like the following..
<Part class="Addition">
  <Parameter1>
    <Part class="Number" Value="5"/>
  </Parameter1>
  <Parameter2>
    <Part class="Subtraction">
      <Parameter1>
        <Part class="Number" Value="7"/>
      </Parameter1>
      <Parameter2>
        <Part class="Number" Value="3"/>
      </Parameter2>
    </Part>
  </Parameter2>
</Part>
this would return a 9 - "( 5 + ( 7 - 3 ))"
Where I'm stuck is finding a way to specify which parts are imported for the parameters at run time.
I was thinking of adding a GUID to each class in the metadata, this could be used in the config file to specify the part.
But where do I go from there? Do I need to look into custom ExportProvider

 

 


 

May 18, 2010 at 7:31 PM

Try following
[Import(typeof(INumber),"Parameter1")]
Or 
[Import(typeof(INumber, "Operand1"))]


May 18, 2010 at 7:37 PM

http://mef.codeplex.com/wikipage?title=Exports%20and%20Metadata&referringTitle=Guide