How to configure unity container to provide string constructor value?

后端 未结 1 814
太阳男子
太阳男子 2021-02-07 07:49

This is my dad class

 public class Dad
    {
        public string Name
        {
            get;set;
        }
        public Dad         


        
1条回答
  •  时光取名叫无心
    2021-02-07 08:42

    You should provide a parameterless constructor:

    public class Dad
    {
        public string Name { get; set; }
    
        public Dad()
        {
        }
    
        public Dad(string name)
        {
            Name = name;
        }
    }
    

    If you can't provide a parameterless constructor, you need to configure the container to provide it, either by directly registering it with the container:

    UnityContainer DadContainer = new UnityContainer();
    DadContainer.RegisterType(
        new InjectionConstructor("chris"));
    

    or through the app/web.config file:

    
      

    0 讨论(0)
提交回复
热议问题