I noticed in C #, unlike C ++, you can combine virtual and general methods. For example:
using System.Diagnostics; class Base { public virtual void Concrete() {Debug.WriteLine("base concrete");} public virtual void Generic<T>() {Debug.WriteLine("base generic");} } class Derived : Base { public override void Concrete() {Debug.WriteLine("derived concrete");} public override void Generic<T>() {Debug.WriteLine("derived generic");} } class App { static void Main() { Base x = new Derived(); x.Concrete(); x.Generic<PerformanceCounter>(); } }
Given that any number of Generic<T> versions can be created, it does not look like the standard vtbl method can be used to resolve method calls, but in fact it is not. Here is the generated code:
x.Concrete(); mov ecx,dword ptr [ebp-8] mov eax,dword ptr [ecx] call dword ptr [eax+38h] x.Generic<PerformanceCounter>(); push 989A38h mov ecx,dword ptr [ebp-8] mov edx,989914h call 76A874F1 mov dword ptr [ebp-4],eax mov ecx,dword ptr [ebp-8] call dword ptr [ebp-4]
Extra code seems to be looking for dynamic vtbl according to general parameters, and then calls it. Has anyone written about the features of this implementation? How well does it work compared to the non-general case?
performance generics polymorphism c #
zildjohn01
source share