我有两个带有导出的类:
[Export(typeof(Mod))]
public class CoreMod : Mod
{
[ImportingConstructor]
public CoreMod()
{
//here goes my constructor
}
}
[Export(typeof(Mod))]
public class AnotherMod : Mod
{
[ImportingConstructor]
public AnotherMod()
{
//here goes my constructor
}
}
CoreMod
是在我的主程序集中,AnotherMod
是在外部程序集中。 Mod
在另一个程序集中,它们都引用了这个程序集。
在我的应用程序中,我有一个类,试图通过MEF加载Mods:
class ModManager
{
[ImportMany(typeof(Mod))]
public static IEnumerable<Mod> Mods { get; set; }
public List<Mod> LoadedMods { get; set; }
public ModManager()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(CoreMod).Assembly));
catalog.Catalogs.Add(new DirectoryCatalog(
Path.GetDirectoryName(
new Uri(Assembly.GetExecutingAssembly()
.CodeBase).LocalPath)));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
LoadedMods = Mods.ToList();
}
}
我觉得所有的导入应该都已经满足了,但它仍然无法导入任何东西(
Mods
为空)。我做错了什么?