Я работаю над некоторыми примерами программ, использующих MEF.В простейшем примере, который я написал, у меня есть IDoSomething
интерфейс, который выглядит так:
public interface IDoSomething
{
void DoIt();
}
и класс, который его реализует, который выглядит так:
[Export(typeof(IDoSomething))]
public class TestClass : IDoSomething
{
public void DoIt()
{
Console.WriteLine("TestClass::DoIt");
}
}
и класс, который загружает плагин, который выглядит следующим образом:
public class PluginManager
{
[ImportMany(typeof(IDoSomething))]
private IEnumerable<IDoSomething> plugins;
public void Run()
{
Compose();
foreach (var plugin in plugins)
{
plugin.DoSomething();
}
}
private void Compose()
{
var catalog = new DirectoryCatalog(@".\");
var compositionContainer = new CompositionContainer(catalog);
compositionContainer.ComposeParts(this);
}
}
И это, кажется, работает, но теперь я хотел бы расширить свои конкретные плагины, чтобы у них был плагин, что-товот так:
[Export(typeof(IDoSomething))]
public class TestClass2 : IDoSomething
{
[Import(typeof(IDoAnotherThing))]
public IDoAnotherThing Plugin { get; set; }
public void DoIt()
{
Console.WriteLine("TestClass2::DoIt");
Plugin.Func();
}
}
где IDoAnotherThing
выглядит так:
public interface IDoAnotherThing
{
void Func();
}
и моя конкретная реализация этого выглядит так:
[Export(typeof(IDoAnotherThing))]
public class AnotherPlugin: IDoAnotherThing
{
public void Func()
{
Console.WriteLine("AnotherPlugin::Func");
}
}
ПоведениеКогда я запускаю, я вижу, что мой экземпляр TestClass2
создается и вызывается его функция DoIt
, но никогда не вызывается его экземпляр AnotherPlugin
.Я вижу, что в каталоге есть AnotherPlugin
в списке.Что я здесь не так делаю?