intentor / adic

Lightweight dependency injection container for Unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Binds that implement IDisposable cannot be injected

FodderMK opened this issue · comments

As of Adic v2.19 the code below throws a "InvalidCastException: Cannot cast from source type to destination type." exception. Adic v2.18 does not throw this exception. I'm able to fix the problem by changing "ToNamespace" to "ToNamespaceSingleton" but I don't actually want the classes derived by ITestInterface to be singletons, just the parent class.

using System;
using Adic;

public class ToNamespaceTest : ContextRoot {
    public override void SetupContainers() {
        var container = this.AddContainer<InjectionContainer>();
        container.RegisterExtension<EventCallerContainerExtension>();

        container.Bind<ITestInterface>().ToNamespace("Test");
        container.Bind<TestParent>().ToSingleton();
    }

    public override void Init() {}
}

public interface ITestInterface {}

public class TestParent {
    [Inject] public ITestInterface Children;
}

namespace Test {
    public class TestChild1 : ITestInterface, IDisposable {
        public void Dispose() {}
    }
}

The code below also throws a "InvalidCastException" in Adic 2.19. Am I doing something incorrect?

using System;
using Adic;
using Adic.Container;

public class RandomThing : ContextRoot
{
    private IInjectionContainer container;

    public override void SetupContainers() {
        container = this.AddContainer<InjectionContainer>();
        container.RegisterExtension<EventCallerContainerExtension>();

        container.Bind<TestChild1>().ToSelf();
        container.Bind<TestParent>().ToSelf();
    }

    public override void Init() {
        this.container.Resolve<TestParent>();
    }
}

public class TestParent {
    [Inject] public TestChild1 Children;
}

public class TestChild1 : IDisposable {
    public void Dispose() {}
}

Hello!

The code seems to be correct. I'll look into it and analyze the issue!

Thanks for the report!

The problem occured because a new checking of already added caller was using types instead of instances.

Fixed on the 2.19.1 version.