Hi all,
My question title sounds a little bit difficult - sorry. I'm new in MEF :-).
My scenario:
public class MainClass
{
[ImportMany(typeof(ITest))]
private List<ITest> Tests { get; set; }
public MainClass()
{
Init();
}
private void Init()
{
DirectoryCatalog catalog = new DirectoryCatalog(@"./");
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
[Export("BirthdayJob")]
[Export(typeof(ITest))]
public partial class BirthdayTest : ITest
{
[ImportingConstructor]
public BirthdayUserControl(IParameter parameter)
{
InitializeComponent();
this.Parameter = jobParameter;
}
public IParameter Parameter { get; set; }
}
[Export(typeof(IParameter))]
[Export("BirthdayParameter")]
public class BirthdayJobParameter : IParameter
{
public override string ToString()
{
return "Birthday Remember";
}
}
public interface IParameter : IMefInterface
{
}
public interface IMefInterface
{
}
In the generic list of test, I should have all possible ITest objects with the associated IParameter object. Unfortunately, there aren't any items in the generic list.
Can you help? What did I do wrong?
Thanks in advance.
Regards, pro
//Edit
So I have a compilable Class for my problem :
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace ObjectContracts
{
public class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer(new TypeCatalog(typeof (IFoo), typeof (Bar), typeof(Foo)));
var bar = container.GetExportedValue<Bar>();
Console.WriteLine(bar.Foo.Message);
Console.ReadLine();
}
}
[InheritedExport]
public interface IFoo
{
string Message { get; set; }
}
[Export]
public class Bar
{
[ImportingConstructor]
public Bar([Import("Foo")]IFoo foo)
{
this.Foo = foo;
}
public IFoo Foo { get; set;}
}
[Export("Foo")]
public class Foo : IFoo
{
public Foo()
{
Message = ":-)";
}
public string Message { get; set; }
}
}
What do I do wrong? Please help me :-)
Regards, patrick