i have some simple Export/Import Scenario that i can’t figur out why this not work. in my scenario i have one WPF app and 2 ClassLibrary, in Classlib1 i have one interface named ITestEx1 as below :
public interface ITestEx1
{
string Name {get; set;}
}
and 1 derived class Named (TestEx1) as below :
using System.ComponentModel.Composition;
[Export(typeof(ITestEx1))]
public class TestEx1 : ITestEx
{
public Name {get; set;}
}
as you can see this class exported as type of ITestEx1, now in Classlib2 i refrenced Classlib1 and have one class as below :
using System.ComponentModel.Composition;
using Classlib1;
public class TestMEF
{
[Import(typeof(ITestEx1))]
public ITestEx1 TestE {get; set;}
}
and in main WPF application i refrenced both Classlib1 and ClassLib2 and in constructor of MainWindow.xaml i wrote this code for initializing MEF :
private CompositionContainer _container;
...
public MainWindow()
{
InitializeComponent();
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MainWindow).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(TestEx1).Assemble));
_container = new CompositionContainer(catalog)
_container.ComposeParts(this);
}
… and in button click i have this :
{
...
var aa = new TestMEF();
aa.TestE.Name = "abc"; // Error, object null refrence
}
Please Help Me to solve this problem
You have to grab the instance out of the container, not create it yourself. In this case, you would need a factory (in the container) to create the objects on-the-fly.