سلام :19:
چطور میشه نوع یک Object رو تشخیص داد ؟ که string یا char یا از نوع listViewItem یا از نوع ArrayList یا ....
با تشکر
Woeful:40:
Printable View
سلام :19:
چطور میشه نوع یک Object رو تشخیص داد ؟ که string یا char یا از نوع listViewItem یا از نوع ArrayList یا ....
با تشکر
Woeful:40:
با استفاده از تابع GetType می شه فهمید
سلام
دو راه برای این تشخیص وجود دارد که یکی را دوستمان L u K e ! بیان کردند... و مستقیماً یا با کمک کلمات کلیدی typeof در #C و GetType در VB هم قابل استفاده است.
راه دیگر اپراتور is در #C و TypeOf در VB است.
چند نمونه ...
کد://C#.Net
object v = ...
if (v is int)
{
//...
}
else if (v is string)
{
//...
}
else if (v is System.Collections.Hashtable)
{
//...
}
if (v != null)
{
Type type = v.GetType();
if (type == typeof(int))
{
//...
}
else if (type == typeof(string))
{
//...
}
else if (type == typeof(System.Collections.Hashtable))
{
//...
}
}
if (v != null)
{
switch (v.GetType().FullName)
{
case "System.Int32":
//...
break;
case "System.String":
//...
break;
case "System.Collections.Hashtable":
//...
break;
}
}
'VB.Net
Dim v As Object = ...
If (TypeOf v Is Integer) Then
'...
ElseIf (TypeOf v Is String) Then
'...
ElseIf (TypeOf v Is System.Collections.Hashtable) Then
'...
End If
If (v IsNot Nothing) Then
Dim type As Type = v.GetType()
If (type Is GetType(Integer)) Then
'...
ElseIf (type Is GetType(String)) Then
'...
ElseIf (type Is GetType(System.Collections.Hashtable)) Then
'...
End If
End If
If (v IsNot Nothing) Then
Select Case v.GetType().FullName
Case "System.Int32"
'...
Case "System.String"
'...
Case "System.Collections.Hashtable"
'...
End Select
End If