Class.GetType() :对非共享成员的引用需要 VB.net 中的对象引用
Class.GetType() : Reference to a non-shared member requires an object reference in VB.net
我希望这不是重复的,这是我的代码的简化示例:
Public Class ClassUsingFriendClassComparerCustomCollection
' Blah blah members blah blah methods
Private collection As ArrayList
Public Sub sort() _
Implements FNUICollection.sort
Me.collection.Sort(New ComparerCustomCollection)
End Sub
End Class
Friend Class ComparerCustomCollection
Implements IComparer
Public Enum StuffCases
GoForStuff
GoForOtherStuff
End Enum
Private Function getCase(type as System.Type) As StuffCases
' Blah blah returns StuffCases value
End Function
Private Function relevantFunction(x as ACustomObject) as Boolean
' Select Case Me.getCase(ACustomObject.GetType()) ' Fails :(
Select Case Me.getCase(Case x.GetType()) ' Works :)
Case GoForStuff
Me.doSomeStuffWith(x)
Case GoForOtherStuff
Me.doSomeOtherStuffWith(x)
End Select
End Function
End Class
我不明白为什么注释行失败...Visual Studio 报告:
Reference to a non-shared member requires an object reference in VB.net
我是不是漏掉了一件显而易见的事情?
它失败了,因为 ACustomObject
是一种类型,而 x
是该类型的对象。 Type.GetType()
。在您的情况下,可能的使用方式如下:
x.GetType()
或者
Type.GetType("ACustomObject")
我希望这不是重复的,这是我的代码的简化示例:
Public Class ClassUsingFriendClassComparerCustomCollection
' Blah blah members blah blah methods
Private collection As ArrayList
Public Sub sort() _
Implements FNUICollection.sort
Me.collection.Sort(New ComparerCustomCollection)
End Sub
End Class
Friend Class ComparerCustomCollection
Implements IComparer
Public Enum StuffCases
GoForStuff
GoForOtherStuff
End Enum
Private Function getCase(type as System.Type) As StuffCases
' Blah blah returns StuffCases value
End Function
Private Function relevantFunction(x as ACustomObject) as Boolean
' Select Case Me.getCase(ACustomObject.GetType()) ' Fails :(
Select Case Me.getCase(Case x.GetType()) ' Works :)
Case GoForStuff
Me.doSomeStuffWith(x)
Case GoForOtherStuff
Me.doSomeOtherStuffWith(x)
End Select
End Function
End Class
我不明白为什么注释行失败...Visual Studio 报告:
Reference to a non-shared member requires an object reference in VB.net
我是不是漏掉了一件显而易见的事情?
它失败了,因为 ACustomObject
是一种类型,而 x
是该类型的对象。 Type.GetType()
。在您的情况下,可能的使用方式如下:
x.GetType()
或者
Type.GetType("ACustomObject")