views:

36

answers:

1

How to set a Nullable property of a class to null through Windsor configuration file?

Let's say I have a class like this:

public class MyComponent
{
    public int? MyProperty { get; set; }
}

and a configuration file similar to this:

<component id="MyComponent" ...>
   <parameters>
       <MyProperty>!!! what do I put here? !!!</MyProperty>
   </parameters>
</component>

What do I put in the inner text above to set property to null? (yes I know in this example it will actually be null by default, but imagine that it is set in constructor to something else)

Empty text does not work - I've checked.

A: 

It is not possible, but I don't consider this a missing feature. If you don't need it - be explicit about it - create a constructor without this parameter, and Windsor will handle this just fine.

Krzysztof Koźmic
I don't want it as a constructor parameter, because it's just a non obligatory value that has a default set inside the class. I'm completely fine with Windsor not supporting it, I guess I just wanted to convey two things in one property: both weather something should be done (null/non null) and an argument for this thing (value when non null). I can easily just split it into two properties: a boolean (do/don't do something) and the value.
Artur
I agree (again) with Krzysztof but I also think an empty parameter in xml should set the property to null. IMO it's an unexpected behavior not to do so.
Mauricio Scheffer

related questions