title: Compare types if one is nullable
body: <p>I need to check if two types are the same:</p>
  
  <pre><code>private bool AreOfTheSameType(object obj1, object obj2) {
    return obj1.GetType()==obj2.GetType();
  }
  </code></pre>
  
  <p>This works fine with this values:</p>
  
  <pre><code>var test1=AreOfTheSameType(new DateTime(), new DateTime()) // true;
  var test2=AreOfTheSameType(new int(), new DateTime()) // false;
  </code></pre>
  
  <p>What I now want is that the following returns true, too:</p>
  
  <pre><code>var test3=AreOfTheSameType(new int?(), new int()) 
  </code></pre>
  
  <p>So if the types have the same base, but one is nullable, the other isn't, I also want to return it as true. Or to say it in another way I want to have a function that returns whether I can store obj1 into obj2 directly using reflection without having to cast the value.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I reduced my code to make it more readable. Looks like this time that was contra-productive. The real-world-code follows:</p>
  
  <pre><code>var entity = Activator.CreateInstance(typeof(T));
  Type entityType = typeof(T);
  PropertyInfo[] entityProperties = entityType.GetProperties();
  foreach (KeyValuePair&lt;string, object&gt; fieldValue in item.FieldValues)
  {
     if (fieldValue.Value == null) continue;
     var property = entityProperties.FirstOrDefault(prop =&gt; prop.Name == fieldValue.Key);
     if (property != null &amp;&amp; property.CanWrite)
     {
        Type valueType = fieldValue.Value.GetType();
        if (fieldValue.Value.GetType() == property.PropertyType) {
          // Assign
        }
     }
  }
  </code></pre>
  
  <p>The problem on the "//Assign" - line is, I have the following two types:</p>
  
  <pre><code>fieldValue.Value.GetType().ToString()="System.DateTime"
  property.PropertyType.ToString()="System.Nullable`1[System.DateTime]"
  </code></pre>
  
  <p>which are obiously not the same but could be assigned</p>