Je veux utiliser mef pour ajouter des plugins. Et dans le plugin, a-t-il importé quelque chose d'un autre plugin ajouté par le programme principal, comme ioc?L'import MEF n'a rien
maintenant je peux obtenir ce plugin avec succès, mais dans le plugin ne peut pas obtenir l'importé.
Voici mon code. le log in plugin.Service est null.
Interfaces projet
Ilog.cs
public interface Ilog
{
void Log(string log);
}
IServece.cs
public interface IService
{
Ilog Log { get; }
void DoSomeThing();
}
IPlugin.cs
public interface IPlugin
{
string PluginName { get; }
IService Service { get; }
}
plugin1 Projet
public class Plugin1Service : IService
{
[Import(typeof(Ilog))]
public Ilog Log { get; private set; }//todo:Log is null
public void DoSomeThing()
{
Log.Log("test from Plugin1");
}
}
[Export(typeof(IPlugin))]
public class Plugin1Info : IPlugin
{
public string PluginName => "Plugin1";
public IService Service =>new Plugin1Service();
}
Consolelog porject
[Export(typeof(Ilog))]
public class Console1 : Ilog
{
public void Log(string log)
{
Console.WriteLine(log);
}
}
Projet principal
class Program
{
static void Main(string[] args)
{
var catalogs = new DirectoryCatalog(Directory.GetCurrentDirectory());
var container = new CompositionContainer(catalogs);
var plugin = container.GetExportedValue<IPlugin>();
Console.WriteLine($"{plugin.PluginName}");
plugin.Service.DoSomeThing();
Console.ReadKey(true);
}
}
lors de l'exécution du programme, la connexion plugin.Service est null (peut voir 'todo:'). Comment puis-je obtenir l'article de journal?
Réservoirs à @Pidon, et j'ai compris. – user7359612