A strongly typed generic method calls the base class method of the argument, not the shadow method in T?

Consider a class MyFormthat contains a hidden implementation Show(). It also contains a method CreateForm()that takes an instance of the form and calls a shaded sub:

Public Class MyForm
    Inherits Form

    Public Shadows Sub Show()
        MessageBox.Show("Shadowed implementation called!")
    End Sub
End Class

...

Public Sub CreateForm(ByVal childForm As MyForm)
    childForm.MdiParent = Me
    childForm.Show()
    childForm.Focus()
End Sub

When called with CreateForm(New MyForm()), the shadow implementation is correctly called Show(). Now consider the following general implementation:

Public Sub CreateForm(Of T As Form)(ByVal childForm As T)
    childForm.MdiParent = Me
    childForm.Show()
    childForm.Focus()
End Sub

It called CreateForm(Of MyForm)(New MyForm())this strongly typed general method never causes shadowed method.

Is this a mistake, or am I missing something?

+3
source share
3 answers

" ". , , , ( , ++). , , T Form. MyForm , , Form.

, Shadows , Shadow . , Form ( MyForm). Overridable, .

+3

- . , (, !). , , () , .

. "" "" VB.NET - .

+2

, , , T, Form. , , (.. Form).

+1

Source: https://habr.com/ru/post/1770398/


All Articles