C#中反思:如何获得可空&LT的类型; INT&GT;? [英] C# Reflection: How to get the type of a Nullable<int>?
本文介绍了C#中反思:如何获得可空&LT的类型; INT&GT;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的是这样的:
switch( myObject.GetType().GetProperty( "id") )
{
case ??:
// when Nullable<Int32>, do this
case ??:
// when string, do this
case ??:
// when Nullable<bool>, do this
什么路径object.GetType下()将不得不使用一个case语句,我可以比较的数据类型的字符串名称?我需要知道的类型,所以我可以有很多Convert.ToInt32(串),将设置使用反射myObject的价值之一。
What path under object.GetType() would have the string name of the datatype that I could compare using a case statement? I need to know the type so I can have one of many Convert.ToInt32( string ) that will set the value of myObject using Reflection.
推荐答案
您并不需要一个字符串名称来比较一下:
You don't need a string name to compare it:
if (myObject.GetType() == typeof(Nullable<Int32>))
// when Nullable<Int32>, do this
else if (myObject.GetType() == typeof(string))
// when string, do this
else if (myObject.GetType() == typeof(Nullable<bool>))
// when Nullable<bool>, do this
编辑:更改switch语句,如果链
Changed switch statement to if chain.
这篇关于C#中反思:如何获得可空&LT的类型; INT&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文