インポートが機能していません-オブジェクトがnullです。元々はImportManyでしたが、問題を特定するためにImportに簡略化しましたが、うまくいきませんでした。
私はこのサイトとGoogleを調べて、主なアイデアに従いました。
- クラスを自分でインスタンス化しないでください。MEFにインスタンス化させてください。そうしないと、container.getExport()を呼び出します-それでも機能しません
- [Import]プロパティを含むクラスに[Export]を配置します。そうしないと、コンテナ構成プロセスによって一部として取得されません(デバッグ時に確認されます)。
私のコード設定は次のとおりです(コンパクトにするために簡略化されています)。
アセンブリ1
public class MyBootstrapper
{
//Automatically called by ExcelDna library, I do not instantiate this class
public void AutoOpen()
{
var ac1 = new AssemblyCatalog(typeof(XLHandler).Assembly);
var ac2 = new AssemblyCatalog(typeof(MyComponent).Assembly);
var agc = new AggregateCatalog();
agc.Catalogs.Add(ac1);
agc.Catalogs.Add(ac2);
var cc = new CompositionContainer(agc);
try
{
cc.ComposeParts(this);
}
catch (CompositionException exception) {}
}
}
[Export]
public class XLHandler
{
[Import(typeof(IMyComponent))]
public IMyComponent _component;
public void SomeMethod()
{
//try to use _component but it is null
}
}
アセンブリ2
public interface IMyComponent
{
//stuff...
}
アセンブリ3
[Export(typeof(IMyComponent)]
public MyComponent : IMyComponent
{
//more stuff...
}
XLHandlerの_component変数がMEFコンテナによって注入されない理由を知っている/知っている人はいますか?
Assembly2のインターフェイスのAssemblyCatalogをエクスポート/作成する必要がありますか?