menjaraz
menjaraz

Reputation: 7575

How can I get the actual generic parameter outside the generic class?

Here is a stripped down snippet of a project of mine :

Interface part :

type
  TMainFormView<T:TCustomForm> = class(TInterfacedObject, IMainFormView)
  private
    ...
  protected
    function GetMainFormClass: TCustomFormClass;
    //
    property MainFormClass:TCustomFormClass read GetMainFormClass;
  public
    ...
  end;

Implementation part:

function TMainFormView<T>.GetMainFormClass: TCustomFormClass;
begin
  Result:= T;
end;

I've already refactored it and the former implementation was very ugly believe me (I used an inner class which I found out lately to be an overkill and not elegant).

Is there other better way to code it? My purpose is to be able to extract the "concrete" class of T and expose it through protected property.

Upvotes: 1

Views: 216

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

The question seems a little vague but I understand it that you are asking for a slicker way to query an instance for the value of T, the type argument of the instantiated type. The only way I know to do this is via a method, exactly as you have done it. I think you already have the best solution.

Upvotes: 1

Related Questions