Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Wow thanks this question/answer really helped me out. I heart Stackoverflow.</p> <p>I made what you are doing above a little more generic. All we're really looking for is to have Nullable with slightly different serialization behavior. I used Reflector to build my own Nullable, and added a few things here and there to make the XML serialization work the way we want. Seems to work pretty well:</p> <pre><code>public class Nullable&lt;T&gt; { public Nullable(T value) { _value = value; _hasValue = true; } public Nullable() { _hasValue = false; } [XmlText] public T Value { get { if (!HasValue) throw new InvalidOperationException(); return _value; } set { _value = value; _hasValue = true; } } [XmlIgnore] public bool HasValue { get { return _hasValue; } } public T GetValueOrDefault() { return _value; } public T GetValueOrDefault(T i_defaultValue) { return HasValue ? _value : i_defaultValue; } public static explicit operator T(Nullable&lt;T&gt; i_value) { return i_value.Value; } public static implicit operator Nullable&lt;T&gt;(T i_value) { return new Nullable&lt;T&gt;(i_value); } public override bool Equals(object i_other) { if (!HasValue) return (i_other == null); if (i_other == null) return false; return _value.Equals(i_other); } public override int GetHashCode() { if (!HasValue) return 0; return _value.GetHashCode(); } public override string ToString() { if (!HasValue) return ""; return _value.ToString(); } bool _hasValue; T _value; } </code></pre> <p>You lose the ability to have your members as int? and so on (have to use Nullable&lt;int&gt; instead) but other than that all behavior stays the same.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload