Skip to content

Attribute [Import] is populated twice #277

Description

@mgth

I could not tell until now, but I have a case where Injection has side effects and I noticed that injection within [Import] marked methods occurs twice

    public class Exported
    { }

    public interface ITest
    {
        int P { get; }
        int I { get; }
        int C { get; }
    }

    [Export(typeof(ITest))]
    public class Test : ITest
    {
        public int P { get; set; } = 0;
        private Exported _exported;

        [Import]
        public Exported InjectProperty
        {
            get => _exported;

            set
            {
                _exported = value;
                P++;
            }
        }

        public int I { get; set; } = 0;

        [Import]
        public void InjectMethod(Exported exported)
        {
            I++;
        }

        public int C { get; set; } = 0;
        public Test()
        {
            C++;
        }
    }

    public class GraceTest
    {
        [Fact]
        public void Test()
        {
            var container = new DependencyInjectionContainer();

            var assembly = Assembly.GetAssembly(typeof(Test));

            container.Configure(c => c
                .ExportAssembly(assembly).ExportAttributedTypes()
            );

            var test = container.Locate<ITest>();

            Assert.Equal(1,test.C);
            Assert.Equal(1,test.P);
            Assert.Equal(1,test.I);
        }
    }

Activity

  1. added a commit that references this issue on May 2, 2021
  2. ipjohnson commented on May 2, 2021

    @ipjohnson
    Owner

    Ok I've pushed a fix for this to nuget as prerelease

  3. mgth commented on May 2, 2021

    @mgth
    Author

    Fine, works great, thanks a lot.

  4. ipjohnson commented on May 2, 2021

    @ipjohnson
    Owner

    cool, I'm going to close this out

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions