【发布时间】:2014-07-31 16:58:06
【问题描述】:
我有一个数组列表,其中包含 不同类型的值,第一个值->字符串,第二个值-> 日期时间,第三个value--> 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);
【问题讨论】:
-
在类型前面添加 typeof(),例如类型(字符串),类型(日期时间)。
-
'2.0' 本身是一个非常糟糕的标签选择。今后请多注意问题的自动提示提示:正确的标记是合格的人如何找到您的问题。
-
如果可以,请摆脱这种情况。数组列表是表示四元组数据的糟糕方式。考虑定义一个包含这四个数据的自定义类并改用它。