Passing parameters to constructors using Autofac

前端 未结 4 1464
说谎
说谎 2021-01-31 02:42

I\'m very new to autofac so it\'s possible that I\'m completely misusing it.

Let\'s say I have a class that has this structure:

public class HelperClass          


        
4条回答
  •  温柔的废话
    2021-01-31 02:51

    You can always use the WithParameter method to explicitly specify a constructor parameter:

    builder.RegisterType()
           .As()
           .WithParameter("helper", new HelperClass("do", "something"));
    
    builder.RegisterType()
           .As()
           .WithParameter("helper", new HelperClass("do", "somethingelse"));
    

    As far as I can tell there is no need for an interface for HelperClass because it essentially is just a value holder.

    For this to work you would need to make the internal constructor public, I think.

提交回复
热议问题