信息
您所需的主题如下所示。但此主题未包含在此库中。

Object.GetType 方法

2013/12/13

获取当前实例的 Type

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

声明
Public Function GetType As Type

返回值

类型: System.Type
Type 实例,表示当前实例的确切运行时类型。

对于具有相同运行时类型的两个对象 xyObject.ReferenceEquals(x.GetType(),y.GetType()) 返回 true

Type 对象公开与当前 Object 的类关联的元数据。

下面的代码示例说明 GetType 返回当前实例的运行时类型。



' Example base class and derived class. Note that it
' is not necessary to derive from Object explicitly;
' a class with no Inherits statement implicitly 
' derives from Object.
'
Public Class MyBaseClass
   Inherits Object
End Class

Public Class MyDerivedClass
   Inherits MyBaseClass
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim base As New MyBaseClass()
      Dim derived As New MyDerivedClass()
      Dim o As Object = derived
      Dim b As MyBaseClass = derived

      outputBlock.Text += String.Format("base.GetType returns {0}", base.GetType()) & vbCrLf
      outputBlock.Text += String.Format("derived.GetType returns {0}", derived.GetType()) & vbCrLf
      outputBlock.Text += String.Format("Dim o As Object = derived; o.GetType returns {0}", o.GetType()) & vbCrLf
      outputBlock.Text += String.Format("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType()) & vbCrLf

   End Sub
End Class

'This code example produces the following output:
'
'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
'


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: