Windsor Container: How to specify a public property should not be filled by the container?

后端 未结 7 1921
小鲜肉
小鲜肉 2021-01-01 23:32

When Instantiating a class, Windsor by default treats all public properties of the class as optional dependencies and tries to satisfy them. In my case, this creates a rath

7条回答
  •  遥遥无期
    2021-01-01 23:46

    I do not know which version of Castle you guys were using at that time, but none of the solution mentioned were working. Plus, there is a lot of dead links.

    With castle 3.1, here the solution I came up with (thanks to some castle source code digging):

    container.Register(Component.For(type)
                                            .LifestyleTransient()
                                            .Properties( propertyInfo => propertyInfo.PropertyType != typeof(MyOtherType)));
    

    The 'Properties' function adds a property filter used by castle when constructing the ComponentModel. In my case, all properties dependency will be satisfied except the property type 'MyOtherType'.

提交回复
热议问题