Class.GetType():对非共享成员的引用需要VB.net中的对象引用

Class.GetType() : Reference to a non-shared member requires an object reference in VB.net

我希望这不是重复的,这是我的代码的简化示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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()。 在您的情况下,可以使用以下方式:

1
x.GetType()

要么

1
Type.GetType("ACustomObject")