virtual class methods

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > class methods > virtual class methods

Delphi supports virtual class methods:

 

type

  TBase = class(TObject)

  public

    class function ClassVirtual(X: Integer): Integer; virtual;

  end;

 

  TDerived = class(TBase)

  public

    class function ClassVirtual(X: Integer): Integer; override;

  end;

 

C# static methods cannot be overridden polymorphically in the same way. Therefore Delphi2C# does not model virtual class methods as ordinary polymorphic C# static methods.

 

Instead, Delphi2C# represents virtual class-method dispatch through TClass.

 

A Delphi class expression such as:

 

TDerived

 

is represented as a runtime metaclass value:

 

TClass.Of<TDerived>()

 

A virtual class-method call through a class type:

 

I := TDerived.ClassVirtual(1);

 

can be translated to:

 

I = TClass.Of<TDerived>().InvokeClassMethod<int>("ClassVirtual", 1);

 

A virtual class-method call through an object reference:

 

P := TDerived.Create;

I := P.ClassVirtual(1);

 

can be translated to:

 

I = P.ClassType().InvokeClassMethod<int>("ClassVirtual", 1);

 

Here P.ClassType() returns a TClass object representing the actual runtime class of P. The call is then dispatched through the metaclass.

 

This preserves the important Delphi behavior: the method implementation is selected according to the class on which the virtual class method is invoked.

 

Example:

 

var

  P: TBase;

begin

  P := TDerived.Create;

  I := P.ClassVirtual(1);

end;

 

Generated C#:

 

TBase P;

int I;

 

P = new TDerived();

I = P.ClassType().InvokeClassMethod<int>("ClassVirtual", 1);

 

The actual class of P is TDerived, so the TDerived implementation of ClassVirtual is invoked.

 

Implementing virtual class methods in C#

 

The method bodies themselves are generated as static methods, but virtual dispatch is performed by TClass.

 

Example Delphi code:

 

class function TBase.ClassVirtual(X: Integer): Integer;

begin

  Result := X;

end;

 

class function TDerived.ClassVirtual(X: Integer): Integer;

begin

  Result := X + 1;

end;

 

Generated C# code:

 

public class TBase : TObject

{

    public static int ClassVirtual(int X)

    {

        int result;

 

        result = X;

 

        return result;

    }

}

 

public class TDerived : TBase

{

    public static new int ClassVirtual(int X)

    {

        int result;

 

        result = X + 1;

 

        return result;

    }

}

 

A direct non-polymorphic call may still be generated as a normal static call when the Delphi semantics do not require virtual class-method dispatch:

 

I = TBase.ClassVirtual(1);

 

But if the Delphi call is virtual in nature, Delphi2C# uses TClass dispatch:

 

I = TClass.Of<TBase>().InvokeClassMethod<int>("ClassVirtual", 1);

I = TClass.Of<TDerived>().InvokeClassMethod<int>("ClassVirtual", 1);

I = P.ClassType().InvokeClassMethod<int>("ClassVirtual", 1);

 

The runtime method used for this purpose is implemented in TClass:

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content