Search code examples
delphifiremonkey

how to access protected class method


we have this base class :

TCustomContextOpenGL = class(TContext3D)
  protected
    **class** procedure CreateSharedContext; virtual; abstract;
  end;

and in the program to know the current context class we do :

TContextManager.DefaultContextClass => return TContextClass = class of TContext3D;

that will for exemple return TCustomAndroidContext or TCustomContextIOS who override CreateSharedContext but let it protected

my problem is that i need to do

TContextManager.DefaultContextClass.CreateSharedContext 

But of course this will not work because CreateSharedContext is protected in TCustomContextOpenGL :( how can i do ?


Solution

  • Best of all is to avoid direct call of protected method. If it is 3th party class and you can't change it, then you can access protected class methods same way as any other protected class member.

    There is example how to access protected object events: Accessing protected event of TWinControl

    Similar way you can access protected class methods:

    type
      TCustomContextOpenGLHack = class(TCustomContextOpenGL);
      CCustomContextOpenGLHack = class of TCustomContextOpenGLHack;
    
    procedure Test;
    begin
      CCustomContextOpenGLHack(TContextManager.DefaultContextClass).CreateSharedContext;
    end;