为什么重载成员函数的行为与非成员函数的行为不同

Why does the behavior of overloaded member functions differ from the behavior of non-member functions

我注意到传递给全局函数的参数总是使用"call",但是重载的成员函数调用总是使用"callvirt"。

当 class 被传递到全局函数 (Print()) 时,为什么它总是调用属于基 class 而不是派生 class 的重载函数?

它似乎在编译时选择了 Print 的 运行 的重载函数,我是否做了一些特别的事情来让它决定不应该在 运行 时解决这个问题?

下面是一些演示我的意思的代码:

Module Module1

    Class BaseClass
        Friend Overridable Sub Print()
            Console.WriteLine("BaseClass.Print")
        End Sub
    End Class

    Class DerivedClass
        Inherits BaseClass

        Friend Overrides Sub Print()
            Console.WriteLine("DerivedClass.Print")
        End Sub
    End Class

    Sub Print(iObject As Object)
        Console.WriteLine("Object")
    End Sub

    Sub Print(iClass1 As BaseClass)
        Console.WriteLine("BaseClass")
    End Sub

    Sub Print(iClass2 As DerivedClass)
        Console.WriteLine("DerivedClass")
    End Sub

    Sub Main()
        Dim tBaseClass As New BaseClass
        Dim tDerivedClass As New DerivedClass
        Dim tBaseClassRef As BaseClass
        Dim tObjPtr As Object

        Console.WriteLine()
        Console.WriteLine("Test 1")
        Console.WriteLine()

        'in IL it always uses callvirt for an overloaded member function

        'from MSDN:     The callvirt instruction calls a late-bound method on an object. 
        '               That is, the method is chosen based on the runtime type of obj rather than the compile-time class visible in the method pointer.

        'prints "BaseClass.print"
        'callvirt   instance void Overloading.Module1/BaseClass::Print()
        tBaseClass.Print()

        'in IL it uses "call", even though Print() is overloaded? Why is this?

        'We slip by here because this type is not late bound

        'prints "BaseClass"
        'call       void Overloading.Module1::Print(class Overloading.Module1/BaseClass)
        Print(tBaseClass)




        Console.WriteLine()
        Console.WriteLine("Test 2")
        Console.WriteLine()

        'prints "DerivedClass.print"
        'callvirt   instance void Overloading.Module1/BaseClass::Print()
        tDerivedClass.Print()

        'call works out okay here too because we're still not late bound

        'prints "DerivedClass"
        'call       void Overloading.Module1::Print(class Overloading.Module1/BaseClass)
        Print(tDerivedClass)




        Console.WriteLine()
        Console.WriteLine("Test 3")
        Console.WriteLine()

        tBaseClassRef = tBaseClass



        tBaseClassRef.Print()
        'prints "BaseClass.print"
        'callvirt   instance void Overloading.Module1/DerivedClass::Print()

        'call took our word for it that tBaseClassRef is BaseClass typed
        'which is correct

        Print(tBaseClassRef)
        'prints "BaseClass"
        'call       void Overloading.Module1::Print(class Overloading.Module1/DerivedClass)



        Console.WriteLine()
        Console.WriteLine("Test 4")
        Console.WriteLine()


        tBaseClassRef = tDerivedClass



        tBaseClassRef.Print()
        'prints "DerivedClass.print"
        'IL_0098:  callvirt   instance void Overloading.Module1/BaseClass::Print()

        'Callvirt correctly handles our tBaseClass having a derived class's type


        Print(tBaseClassRef)
        'prints "BaseClass" <!>

        'IL_009f:  call       void Overloading.Module1::Print(class Overloading.Module1/BaseClass)

        '"Call" is ill-equipped to handle our Derived class

        Console.WriteLine()
        Console.WriteLine("Test 5")
        Console.WriteLine()

        tObjPtr = tDerivedClass

        tObjPtr.Print() 
            '(I don't expect this to work, but the error message surprised me)
            '   -- unhandled exception -- "Public member 'Print' on type 'DerivedClass' <??> not found.

        'IL instructions: http://en.wikipedia.org/wiki/List_of_CIL_instructions

        'a. where is it getting DerivedClass from for the exception? 
        'b. Why did LateCall know what type it was, but was unable to find the method?

        '  [ILDASM]
        '  IL_00be:  ldloc.3    //Load local [3] object tObjPtr)
        '  IL_00bf:  ldnull     // push NULL to stack (no string)
        '  IL_00c0:  ldstr      "Print" // push string object for literal string -- is this what we're using as our object?
        '  IL_00c5:  ldc.i4.0   //Push False
        '  IL_00c6:  newarr     [mscorlib]System.Object
        '  IL_00cb:  ldnull     // no string array
        '  IL_00cc:  ldnull     // no class array
        '  IL_00cd:  ldnull     // no bool array
        '  IL_00ce:  ldc.i4.1   //Push True
        '  IL_00cf:  call       object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.NewLateBinding::LateCall(object,
        '                                                                                                                    class [mscorlib]System.Type,
        '                                                                                                                    string,
        '                                                                                                                    object[],
        '                                                                                                                    string[],
        '                                                                                                                    class [mscorlib]System.Type[],
        '                                                                                                                    bool[],
        '                                                                                                                    bool)

        Console.WriteLine("Marker") 'divider so I can figure out what it is in idasm

        Print(tObjPtr)                                                                                                           
        ' prints "object"

        'again, why use "call" instead of "callvirt"? There is a more specific definition of Print that can handle this
        '[ILDASM]
        '   IL_00e0:  ldloc.3
        '   IL_00e1:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
        '   IL_00e6:  call       void Overloading.Module1::Print(object)

    End Sub

End Module

编辑:另外,请分享您的资源,我想了解更多有关 IL 工作原理的信息。如果重要的话,这是使用 .NET 4.0。

The callvirt instruction calls a late-bound method on an object.

这意味着:在该方法的实例对象上"belongs";在模块方法的情况下(共享方法,如果它是 class)不涉及实例 -> 不需要 callvirt。

这里有 2 个概念,重载和 overriding(这是重载的一种特定形式);只有后者才真正需要 callvirt。

至于怎么选择Print overload你可以看related documentation