當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Object.GetType方法代碼示例

本文整理匯總了VB.NET中System.Object.GetType方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Object.GetType方法的具體用法?VB.NET Object.GetType怎麽用?VB.NET Object.GetType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。


在下文中一共展示了Object.GetType方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: MyBaseClass

' Define a base and a derived class.
Public Class MyBaseClass
End Class 

Public Class MyDerivedClass : Inherits MyBaseClass
End Class 

Public Class Test
    Public Shared Sub Main() 
        Dim base As New MyBaseClass()
        Dim derived As New MyDerivedClass()
        Dim o As Object = derived
        Dim b As MyBaseClass = derived
        
        Console.WriteLine("base.GetType returns {0}", base.GetType())
        Console.WriteLine("derived.GetType returns {0}", derived.GetType())
        Console.WriteLine("Dim o As Object = derived; o.GetType returns {0}", o.GetType())
        Console.WriteLine("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType())
    End Sub 
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:20,代碼來源:Object.GetType

輸出:

base.GetType returns MyBaseClass
derived.GetType returns MyDerivedClass
Dim o As Object = derived; o.GetType returns MyDerivedClass
Dim b As MyBaseClass = derived; b.Type returns MyDerivedClass

示例2: Example

Module Example
   Public Sub Main()
      Dim n1 As Integer = 12
      Dim n2 As Integer = 82
      Dim n3 As Long = 12
      
      Console.WriteLine("n1 and n2 are the same type: {0}",
                        Object.ReferenceEquals(n1.GetType(), n2.GetType()))
      Console.WriteLine("n1 and n3 are the same type: {0}",
                        Object.ReferenceEquals(n1.GetType(), n3.GetType()))
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:12,代碼來源:Object.GetType

輸出:

n1 and n2 are the same type: True
n1 and n3 are the same type: False

示例3: Example

Module Example
   Public Sub Main()
      Dim values() As Object = { 12, CLng(10653), CByte(12), 
                                 CSbyte(-5), 16.3, "string" } 
      For Each value In values
         Dim t AS Type = value.GetType()
         If t.Equals(GetType(Byte))
            Console.WriteLine("{0} is an unsigned byte.", value)
         ElseIf t.Equals(GetType(SByte))
            Console.WriteLine("{0} is a signed byte.", value)
         ElseIf t.Equals(GetType(Integer))   
            Console.WriteLine("{0} is a 32-bit integer.", value)
         ElseIf t.Equals(GetType(Long))   
            Console.WriteLine("{0} is a 32-bit integer.", value)
         ElseIf t.Equals(GetType(Double))
            Console.WriteLine("{0} is a double-precision floating point.", 
                              value)
         Else
            Console.WriteLine("'{0}' is another data type.", value)
         End If   
      Next      
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:23,代碼來源:Object.GetType

輸出:

12 is a 32-bit integer.
10653 is a 32-bit integer.
12 is an unsigned byte.
-5 is a signed byte.
16.3 is a double-precision floating point.
string' is another data type.


注:本文中的System.Object.GetType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。