{"id":1013,"date":"2010-08-08T12:01:26","date_gmt":"2010-08-08T11:01:26","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=1013"},"modified":"2010-08-08T12:01:26","modified_gmt":"2010-08-08T11:01:26","slug":"underlying-nullable-type","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/c-sharp\/underlying-nullable-type","title":{"rendered":"Getting encapsulated type of Nullable<T>"},"content":{"rendered":"<p>Just a short tip for today..<\/p>\n<p>Sometimes I&#8217;ve had the requirement to treat value types, where I don&#8217;t know if the <strong>value type is Nullable&lt;T&gt;<\/strong> or not. Regardless of whether the value type is declared as <code>Nullable&lt;T&gt;<\/code> or not, it should everytime be treat as <code>T<\/code>. Thus I&#8217;ve had to find out the underlying type of the <code>Nullable&lt;T&gt;<\/code>.<\/p>\n<p>The following <strong>extension method on <code>Type<\/code><\/strong> realizes this requirement:<\/p>\n<pre class=\"brush:csharp\">public static class TypeExtensions\r\n{\r\n    public static Type GetBareType(this Type dataType)\r\n    {\r\n        if (dataType != null)\r\n        {\r\n            if (dataType.IsGenericType &amp;&amp;\r\n               (dataType.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;)))\r\n            {\r\n                dataType = Nullable.GetUnderlyingType(dataType);\r\n            }\r\n        }\r\n        return dataType;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Just a short tip for today.. Sometimes I&#8217;ve had the requirement to treat value types, where I don&#8217;t know if the value type is Nullable&lt;T&gt; or not. Regardless of whether the value type is declared as Nullable&lt;T&gt; or not, it should everytime be treat as T. Thus I&#8217;ve had to find out the underlying type &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/c-sharp\/underlying-nullable-type\" class=\"more-link\"><span class=\"screen-reader-text\">Getting encapsulated type of Nullable<T><\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[270,271,272],"class_list":["post-1013","post","type-post","status-publish","format-standard","hentry","category-c-sharp","tag-encapsulated-type","tag-nullable","tag-reflection"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=1013"}],"version-history":[{"count":6,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1013\/revisions"}],"predecessor-version":[{"id":1020,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1013\/revisions\/1020"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=1013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=1013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=1013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}