c#: IsNullableType helper class?
<p>Can anyone help?</p>

<p>I have some code that is shared between 2 projects. The code points to a model which basically is a collection of properties that comes from a db.</p>

<p>Problem being is that some properties use nullable types in 1 model and the other it doesn't</p>

<p>Really the dbs should use the same but they don't ..</p>

<p>so for example there is a property called IsAvailble which uses "bool" in one model and the other it uses bool? (nullable type)</p>

<p>so in my code i do the following</p>

<pre><code> objContract.IsAvailble.Value ? "Yes" : "No"   //notice the property .VALUE as its a bool? (nullable type)
</code></pre>

<p>but this line will fail on model that uses a standard "bool" (not nullable) as there is no property .VALUE on types that are NOT nullable</p>

<p>Is there some kind of helper class that i check if the property is a nullable type and i can return .Value  .. otherwise i just return the property.</p>

<p>Anybody have a solution for this?</p>

<p><strong>EDIT</strong></p>

<p>This is what i have now.....  i am checking HasValue in the nullable type version</p>

<p>public static class NullableExtensions
    {
        public static T GetValue(this T obj) where T : struct
        {
            return obj;
        }
        public static T GetValue(this Nullable obj) where T : struct
        {
            return obj.Value;
        }</p>

<pre><code>    public static T GetValue&lt;T&gt;(this T obj, T defaultValue) where T : struct
    {
        return obj;
    }

    public static T GetValue&lt;T&gt;(this Nullable&lt;T&gt; obj, T defaultValue) where T : struct
    {
        if (obj.HasValue)
            return obj.Value;
        else
            return defaultValue;
    }
}
</code></pre>
