镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

BuildUp with InjectionMethod run-time configuration and Diagnostic() throws "Injected method could not be matched with any public methods" #306

Open

Description

@pascal-910

When using Diagnostic() Extension and a run-time configured InjectionMethod the following exception will be thrown when trying to BuildUp an existing Object.
"System.ArgumentException: Injected method SetDependencies() could not be matched with any public methods on type MyClassWithoutAttribute"
An alternatively usage of [InjectionMethod] Attribute however works as expected .

public interface ISomeInterface { }
public class SomeImpl : ISomeInterface { }

public class MyClassWithAttribute
{
    [InjectionMethod]
    public void SetDependencies(ISomeInterface some) { }
}

public class MyClassWithoutAttribute
{
    public void SetDependencies(ISomeInterface some) { }
}

[TestClass]
public class InjectionMethodIssue
{
    [TestMethod]
    // Usage of run-time configuration with 'InjectionMethod' throws an exception
    public void TestWithoutAttributeAndDiagnostic()
    {
        var container = new UnityContainer().AddExtension(new Diagnostic());
        container.RegisterType<ISomeInterface, SomeImpl>();
        container.RegisterType<MyClassWithoutAttribute>(new InjectionMethod("SetDependencies"));
        var instance = new MyClassWithoutAttribute();
        container.BuildUp(instance);
    }

    [TestMethod]
    // Usage of [InjectionMethod] Attribute instead run-time config works (OK)
    public void TestWithAttributeAndDiagnostic()
    {
        var container = new UnityContainer().AddExtension(new Diagnostic());
        container.RegisterType<ISomeInterface, SomeImpl>();
        var instance = new MyClassWithAttribute();
        container.BuildUp(instance);
    }

    [TestMethod]
    // Run-time config WITHOUT Diagnostic also works (OK)
    public void TestWithoutAttributeAndWithoutDiagnostic()
    {
        var container = new UnityContainer();
        container.RegisterType<ISomeInterface, SomeImpl>();
        container.RegisterType<MyClassWithoutAttribute>(new InjectionMethod("SetDependencies"));
        var instance = new MyClassWithoutAttribute();
        container.BuildUp(instance);
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions