.net 如何使用MEF对零件进行分层?
我正在使用MEF编写一些示例程序。在我写的最简单的例子中,我有一个.net 如何使用MEF对零件进行分层?,.net,mef,.net,Mef,我正在使用MEF编写一些示例程序。在我写的最简单的例子中,我有一个IDoSomething接口,看起来是这样的: public interface IDoSomething { void DoIt(); } 实现它的类如下所示: [Export(typeof(IDoSomething))] public class TestClass : IDoSomething { public void DoIt() { Console.WriteLine("Test
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();
}
}
public interface IDoAnotherThing
{
void Func();
}
[Export(typeof(IDoAnotherThing))]
public class AnotherPlugin: IDoAnotherThing
{
public void Func()
{
Console.WriteLine("AnotherPlugin::Func");
}
}
加载插件的类如下所示:
[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();
}
}
public interface IDoAnotherThing
{
void Func();
}
[Export(typeof(IDoAnotherThing))]
public class AnotherPlugin: IDoAnotherThing
{
public void Func()
{
Console.WriteLine("AnotherPlugin::Func");
}
}
其中,i另一个东西
如下所示:
[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();
}
}
public interface IDoAnotherThing
{
void Func();
}
[Export(typeof(IDoAnotherThing))]
public class AnotherPlugin: IDoAnotherThing
{
public void Func()
{
Console.WriteLine("AnotherPlugin::Func");
}
}
我的具体实现如下所示:
[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();
}
}
public interface IDoAnotherThing
{
void Func();
}
[Export(typeof(IDoAnotherThing))]
public class AnotherPlugin: IDoAnotherThing
{
public void Func()
{
Console.WriteLine("AnotherPlugin::Func");
}
}
当我运行它时,我看到的行为是创建了我的
TestClass2
实例,并调用了它的DoIt
函数,但它的另一个plugin
实例从未被调用。我看到目录中列出了另一个插件。我做错了什么 我认为was正确地说明了这一点,结果证明我是正确的,但正如Matthew Abbott在上面评论的那样,事实上还发生了其他事情。在这种情况下,事实是我的具体类插件并不都在同一个目录中,因此,我的应用程序不是正确的。生活和学习。一旦我确定我拥有了所有这些组件中最新的一个,一切都正常。我认为我正确地指定了这一点,结果证明我是正确的,但正如马修·艾伯特(Matthew Abbott)在上面评论的那样,事实上发生了其他事情。在这种情况下,事实是我的具体类插件并不都在同一个目录中,因此,我的应用程序不是正确的。生活和学习。一旦我确定我拥有了所有这些程序集中最新的一个,一切都很好。我在没有任何代码更改的情况下重新创建了这个程序集,它对我来说很好。您已经指定创建了TestClass2
实例,但是没有调用另一个plugin
实例?如果它不能为插件
属性设置导入,那么它就会失败。一定有别的事情发生了。我们能看到更多的代码吗?我在没有任何代码更改的情况下重新创建了它,对我来说效果很好。您已经指定创建了TestClass2
实例,但是没有调用另一个plugin
实例?如果它不能为插件
属性设置导入,那么它就会失败。一定有别的事情发生了。我们能看更多的代码吗?