0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】VContainerで手動でInjectする方法

Posted at

はじめに

この記事は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();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?