Method export fails when using RegistrationBuilder

Jun 7, 2011 at 10:46 PM

I have been experimenting with the latest MEF2 Preview 3 code drop and have been having trouble with method export when I include a RegistrationBuilder instance as the ReflectionContext parameter for an AssemblyCatalog. Here is a small piece of code that demonstrates the fault, which throws an ArgumentException "MethodInfo must be a runtime MethodInfo object." when it tries to satisfy the imports.

  public interface IFoo { }
  public class Class1
  {
    [Import]
    public Func<IFoo> Foo { get; set; }
  }
  public class Factory
  {
    [Export]
    public IFoo Create() { return null; }
  }
  class Program
  {
    static void Main( string[] args )
    {
      var registration = new RegistrationBuilder();
      // do something with registration here

      var catalog = new AssemblyCatalog( Assembly.GetExecutingAssembly(), registration );

      // uncommenting the next line so it doesn't use RegistrationBuilder works as expected.
      //catalog = new AssemblyCatalog( Assembly.GetExecutingAssembly() );

      var container = new CompositionContainer( catalog, CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe );

      var test = new Class1();
      container.SatisfyImportsOnce( test ); // this causes an exception
    }
  }

When using the RegistrationBuilder, is there some requirement to set import or export methods explicitly or is this a bug?

 thanks,

philip

Jun 8, 2011 at 9:30 PM

I've not used any of this stuff, but I have a feeling that you should have something like the following:

var registration = new RegistrationBuilder();   
registration.OfType<Foo>().ImportProperty<IBar>(property => property.Name == "Bar").Export<IFoo>();   
registration.OfType<Bar>().Export<IBar>();   

var catalog = new TypeCatalog(types: new Type[] { typeof(Foo), typeof(Bar) }, reflectionContext: registration);   
var container = new CompositionContainer(catalog);   
var foo = container.GetExportedValue<IFoo>();   

 It seems to me that you would take this approach when using attribute-less registration.