はじめに
この記事はHamster Output Advent Calendar 2025の5日目の記事です。
VContainerを利用している時に、手動でInjectをする方法を探すのに手間取ったので自身のメモとして記事を書きます。(ソースコードを辿って見つけました...)
手動Injectのやり方
LifetimeScopeを変数として取得し、Lifetimescopeが持つContainer変数からInjectを実行します。Injectの引数には注入対象を指定します。
private LifetimeScope _lifetimeScope;
private void ManualInject()
{
_lifetimeScope.Container.Inject(this);
}
LifetimeScopeが複数ある場合、参照したLifetimescopeを間違えるとDIに失敗するので要注意です!
また、Lifetimescopeの破棄も手動で出来ます。
_lifetimeScope.Dispose();