我现在有一个OBJECT型的变量,我想判断它的类型然后做处理,
比如字符串,整形,日期型等,
不同的变量采用不同的处理方式!
在MSDN里它是这么定义的:
[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Function GetType() As Type
但是我TYPE类型好象没有识别它的类型的方法!
假如你有一个对象 obj,这样判断:
if(obj is string)
//是字符串值
else if(obj is int)
//是int类型
else if(obj is DateTime)
//是日期类型
OBJECT本来就是最基本的类型了
其他都从它继承
如果是装箱的
下例显示 object 类型的变量可以如何接受任何数据类型的值,以及 object 类型的变量可以如何通过 .NET 框架在 System.Object 上使用方法。
public class MyClass2
{
public static void Main()
{
object a;
a = 1; // an example of boxing
Console.WriteLine(a);
//你想要的方法吧??
Console.WriteLine(a.GetType());
Console.WriteLine(a.ToString());
Console.WriteLine();
}
}
输出
1
System.Int32
1
VB应该是这样的
If TypeOf Obj Is string Then
是字符串值
ElseIf TypeOf Obj Is IntegerThen
是int类型
ElseIf TypeOf Obj Is DateTimeThen
是日期类型
End If