メインコンテンツまでスキップ

Register using delegate

Instance creation can be delegated to a lambda expression or another method or class.

builder.Register<IFoo>(_ =>
{
var foo = new Foo();
// Do something;
return foo;
}, Lifetime.Scoped);

It can resolve like this:

class ClassA
{
public ClassA(IFoo foo) { /* ...*/ }
}

The first argument that can be used in the expression is IObjectResolver. Using this, we can retrieve and use the registered object.

builder.Register<IFoo>(container =>
{
var serviceA = container.Resolve<ServiceA>();
return serviceA.ProvideFoo();
}, Lifetime.Scoped);

IObjectResolver.Instantiate can also be used to generate GameObjects executed inject.

builder.Register(container =>
{
return container.Instantiate(prefab);
}, Lifetime.Scoped);

See Use Container directory more information.

注記

These delegates will be executed only once during scope construction. If you want to create an instance at any time during runtime, please refer to Register Factory.