How do I convert a System.Type to its nullable version?

前端 未结 4 1390
长发绾君心
长发绾君心 2021-01-30 19:21

Once again one of those: \"Is there an easier built-in way of doing things instead of my helper method?\"

So it\'s easy to get the underlying type from a nullable type,

4条回答
  •  礼貌的吻别
    2021-01-30 20:02

    There isn't anything built in that I know of, as the int?, etc. is just syntactic sugar for Nullable; and isn't given special treatment beyond that. It's especially unlikely given you're attempting to obtain this from the type information of a given type. Typically that always necessitates some 'roll your own' code as a given. You would have to use Reflection to create a new Nullable type with type parameter of the input type.

    Edit: As the comments suggest actually Nullable<> is treated specially, and in the runtime to boot as explained in this article.

提交回复
热议问题