<p>I'm attempting to create a dataset based on the properties of an object. For example, I have an instance of a Person class with properties including ID, Forename, Surname, DOB etc. Using reflection, I'm adding columns to a new dataset based on the object properties:</p>

<pre><code>For Each pi As PropertyInfo In person.GetType().GetProperties()
    Dim column As New DataColumn(pi.Name, pi.PropertyType)
    table.Columns.Add(column)
Next
</code></pre>

<p>My problem is that some of those properies are nullable types which aren't supported by datasets. Is there any way to extract the underlying system type from a nullable type?</p>

<p>Thanks.</p>

[{'answer_id': 36519, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 1, 'selected': False, 'text': "<p>I'm guessing that the problem is recognizing whether the property is nullable or not. In C# you do this with this code:</p>\n\n<pre><code>if (type.IsGenericType &amp;&amp; type.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;))\n</code></pre>\n\n<p>... but I'm not sure what the equivalent of that last clause is in VB.NET.</p>\n"}, {'answer_id': 44108, 'author': 'Ed Schwehm', 'author_id': 1206, 'author_profile': 'https://Stackoverflow.com/users/1206', 'pm_score': 0, 'selected': False, 'text': '<p>@Mendelt Siebenga: You can only call GetType on the value property if the variable is not set to null; otherwise, you\'ll get an exception.</p>\n\n<p>What you want to do is use the "GetValueOrDefault" property and call GetType on that, since you are guaranteed it will not be null. Example:</p>\n\n<pre><code>Dim i As Nullable(Of Integer) = Nothing\nDim t As Type = i.GetValueOrDefault().GetType()\n</code></pre>\n'}, {'answer_id': 44115, 'author': 'Anderson Imes', 'author_id': 3244, 'author_profile': 'https://Stackoverflow.com/users/3244', 'pm_score': 1, 'selected': False, 'text': '<p>You can also use the <code>GetGenericParameters()</code> method on that type.  <code>myNullableObject.GetType().GetGenericParameters()[0]</code> should give you the type of nullable it is (so <code>Guid</code>, <code>Int32</code>, etc.)</p>\n'}, {'answer_id': 752620, 'author': 'Brian MacKay', 'author_id': 16082, 'author_profile': 'https://Stackoverflow.com/users/16082', 'pm_score': 4, 'selected': False, 'text': "<p>Here's your answer, in VB. This may be overkill for your purposes, but it also might be useful to some other folks.</p>\n\n<p>First off, here's the code to find out if you're dealing with a Nullable type:</p>\n\n<pre><code>Private Function IsNullableType(ByVal myType As Type) As Boolean\n    Return (myType.IsGenericType) AndAlso (myType.GetGenericTypeDefinition() Is GetType(Nullable(Of )))\nEnd Function\n</code></pre>\n\n<p>Note the unusual syntax in the GetType. It's necessary. Just doing GetType(Nullable) as one of the commentors suggested did not work for me.</p>\n\n<p>So, armed with that, you can do something like this... Here, in an ORM tool, I am trying to get values into a generic type that may or not be Nullable:</p>\n\n<pre><code>If (Not value Is Nothing) AndAlso IsNullableType(GetType(T)) Then\n    Dim UnderlyingType As Type = Nullable.GetUnderlyingType(GetType(T))\n    Me.InnerValue = Convert.ChangeType(value, UnderlyingType)\nElse\n    Me.InnerValue = value\nEnd If\n</code></pre>\n\n<p>Note that I check for Nothing in the first line because Convert.ChangeType will choke on it... You may not have that problem, but my situation is extremely open-ended.</p>\n\n<p>Hopefully if I didn't answer your question directly, you can cannibalize this and get you where you need to go - but I just implemented this moments ago, and my tests are all passing. </p>\n"}, {'answer_id': 2028329, 'author': 'herzmeister', 'author_id': 90742, 'author_profile': 'https://Stackoverflow.com/users/90742', 'pm_score': 2, 'selected': False, 'text': "<pre><code>Nullable.GetUnderylingType(myType)\n</code></pre>\n\n<p>will return the underlying type or null if it's not a nullable type.</p>\n"}]
['https://Stackoverflow.com/questions/36511', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']