How do I pass a parameter to the constructor using Light Inject?

前端 未结 4 1096
忘了有多久
忘了有多久 2021-01-01 11:41

Does Light Inject allow you to pass parameters to constructor when you resolve? I\'d like to know if both these frameworks do what Unity\'s ResolverOverride or DependencyOve

4条回答
  •  天命终不由人
    2021-01-01 12:06

    Probably the easiest option with Simple Injector is to register with a delegate

    [Test]
    public void Test1()
    {
        Container container = new Container();
    
        container.Register(() => new ClassWithParameter("SomeValue"));
    
        var result = container.GetInstance();
    }
    
    public interface IClassWithParameter { }
    
    public class ClassWithParameter : IClassWithParameter
    {
        public ClassWithParameter(string parameter)
        {
        }
    }
    

    An advanced option for injecting primitive dependencies is detailed here

提交回复
热议问题