本文整理汇总了VB.NET中System.Type.IsAssignableFrom方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Type.IsAssignableFrom方法的具体用法?VB.NET Type.IsAssignableFrom怎么用?VB.NET Type.IsAssignableFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsAssignableFrom方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Collections.Generic
Module Example
Public Sub Main()
Console.WriteLine("Defined Classes:")
Dim room1 As New Room()
Dim kitchen1 As New Kitchen()
Dim bedroom1 As New Bedroom()
Dim guestroom1 As New Guestroom()
Dim masterbedroom1 As New MasterBedroom()
Dim room1Type As Type = room1.GetType()
Dim kitchen1Type As Type = kitchen1.GetType()
Dim bedroom1Type As Type = bedroom1.GetType()
Dim guestroom1Type As Type = guestroom1.GetType()
Dim masterbedroom1Type As Type = masterbedroom1.GetType()
Console.WriteLine("room assignable from kitchen: {0}", room1Type.IsAssignableFrom(kitchen1Type))
Console.WriteLine("bedroom assignable from guestroom: {0}", bedroom1Type.IsAssignableFrom(guestroom1Type))
Console.WriteLine("kitchen assignable from masterbedroom: {0}", kitchen1Type.IsAssignableFrom(masterbedroom1Type))
' Demonstrate arrays:
Console.WriteLine()
Console.WriteLine("Integer arrays:")
Dim array10(10) As Integer
Dim array2(2) As Integer
Dim array22(2, 2) As Integer
Dim array24(2, 4) As Integer
Dim array10Type As Type = array10.GetType
Dim array2Type As Type = array2.GetType
Dim array22Type As Type = array22.GetType
Dim array24Type As Type = array24.GetType
Console.WriteLine("Integer(2) assignable from Integer(10): {0}", array2Type.IsAssignableFrom(array10Type))
Console.WriteLine("Integer(2) assignable from Integer(2,4): {0}", array2Type.IsAssignableFrom(array24Type))
Console.WriteLine("Integer(2,4) assignable from Integer(2,2): {0}", array24Type.IsAssignableFrom(array22Type))
' Demonstrate generics:
Console.WriteLine()
Console.WriteLine("Generics:")
Dim arrayNull(10) As Nullable(Of Integer)
Dim genIntList As New List(Of Integer)
Dim genTList As New List(Of Type)
Dim arrayNullType As Type = arrayNull.GetType
Dim genIntListType As Type = genIntList.GetType
Dim genTListType As Type = genTList.GetType
Console.WriteLine("Integer(10) assignable from Nullable(Of Integer)(10): {0}", array10Type.IsAssignableFrom(arrayNullType))
Console.WriteLine("List(Of Integer) assignable from List(Of Type): {0}", genIntListType.IsAssignableFrom(genTListType))
Console.WriteLine("List(Of Type) assignable from List(Of Integer): {0}", genTListType.IsAssignableFrom(genIntListType))
Console.ReadLine()
End Sub
End Module
Class Room
End Class
Class Kitchen : Inherits Room
End Class
Class Bedroom : Inherits Room
End Class
Class Guestroom : Inherits Bedroom
End Class
Class MasterBedroom : Inherits Bedroom
End Class
输出:
Defined Classes: room assignable from kitchen: True bedroom assignable from guestroom: True kitchen assignable from masterbedroom: False Integer arrays: Integer(2) assignable from Integer(10): True Integer(2) assignable from Integer(2,4): False Integer(2,4) assignable from Integer(2,2): True Generics: Integer(10) assignable from Nullable(Of Integer)(10): False List(Of Integer) assignable from List(Of Type): False List(Of Type) assignable from List(Of Integer): False
示例2: Example
' 导入命名空间
Imports System.IO
Module Example
Public Sub Main()
Dim t As Type = GetType(Stream)
Dim genericT As Type = GetType(GenericWithConstraint(Of ))
Dim genericParam As Type = genericT.GetGenericArguments()(0)
Console.WriteLine(t.IsAssignableFrom(genericParam))
' Displays True.
End Sub
End Module
Public Class GenericWithConstraint(Of T As Stream)
End Class
示例3: Example
' 导入命名空间
Imports System.Collections
Module Example
Public Sub Main()
Dim t As Type = GetType(IEnumerable)
Dim c As Type = GetType(Array)
Dim instanceOfT As IEnumerable
Dim instanceOfC As Integer() = { 1, 2, 3, 4 }
If t.IsAssignableFrom(c) Then
instanceOfT = instanceOfC
End If
End Sub
End Module
示例4: Example
' 导入命名空间
Imports System.Reflection
Imports System.Reflection.Emit
Public Class A
End Class
Module Example
Public Sub Main()
Dim domain As AppDomain = AppDomain.CurrentDomain
Dim assemName As New AssemblyName()
assemName.Name = "TempAssembly"
' Define a dynamic assembly in the current application domain.
Dim assemBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemName,
AssemblyBuilderAccess.Run)
' Define a dynamic module in this assembly.
Dim moduleBuilder As ModuleBuilder = assemBuilder.DefineDynamicModule("TempModule")
Dim b1 As TypeBuilder = moduleBuilder.DefineType("B", TypeAttributes.Public, GetType(A))
Console.WriteLine(GetType(A).IsAssignableFrom(b1))
End Sub
End Module
输出:
True