我有一个数组列表不同类型的值其中,第一个值->string,第二个值->datetime, 第三个值-->boolean第四个值是int,我如何找到它们的类型并相应地分配值,任何帮助将不胜感激:)
这是我的代码:
foreach (object obj in lstTop)
{
if(obj.GetType() == string)
{do this...)
else if(obj.GetType() == DateTime)
{do this....}
else if(obj.GetType() == bool)
{do this....}
else if(obj.GetType() == Int)
{do this....}
}
谢谢你们,我的最终代码:
string Subscription = "";
DateTime issueFirst;
DateTime issueEnd;
foreach (object obj in lstTop)
{
///Type t = obj.GetType();
if (obj is string)
Subscription += obj + ",";
else if (obj is DateTime)
{
Subscription += Convert.ToDateTime(obj).ToShortDateString() + ",";
}
/// else if (t == typeof(DateTime))
}
return ("User Authenticated user name: " + userName + ", Subscription: " + Subscription);
foreach (object obj in lstTop)
{
if(obj is string)
{do this.....}
else if(obj is DateTime)
{do this.....}
else if(obj is bool)
{do this.....}
else if(obj is Int)
{do this.....}
else
{
// always have an else in case it falls through
throw new Exception();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)