Domanda

ho bisogno di determinare se un TRttiMethod è una funzione

finora, ho scritto questa funzione

Function IsFunction(QualifiedName,MethodName:string):Boolean;
Var
  ctx     : TRttiContext;
  lType   : TRttiType;
  lMethod : TRttiMethod;
Begin
    result:=false;
    ctx := TRttiContext.Create;
    lType:=ctx.FindType(QualifiedName);
    if Assigned(lType) then
    begin
       lMethod:=lType.GetMethod(MethodName);
         if Assigned(lMethod) then
           Result:=(lMethod.ReturnType<>nil); //in this line the exception is raised
    end;
End;

ma questa funzione non riescono con questa eccezione Insufficient RTTI available to support this operation. quando i test il seguito

IsFunction('SysUtils.Exception','CreateFmt')

non riesce anche a queste classi e metodi

SysUtils.Exception -> CreateFmt
SysUtils.Exception -> CreateResFmt
SysUtils.Exception -> CreateResFmt
SysUtils.Exception -> CreateFmtHelp
SysUtils.Exception -> CreateResFmtHelp
SysUtils.Exception -> CreateResFmtHelp
SysUtils.TEncoding -> Convert
SysUtils.TEncoding -> Convert
SysUtils.TEncoding -> GetBufferEncoding
SysUtils.TEncoding -> GetBufferEncoding
SysUtils.TEncoding -> GetByteCount
SysUtils.TEncoding -> GetByteCount
SysUtils.TEncoding -> GetByteCount
SysUtils.TEncoding -> GetByteCount
SysUtils.TEncoding -> GetBytes
SysUtils.TEncoding -> GetBytes
SysUtils.TEncoding -> GetBytes
SysUtils.TEncoding -> GetBytes
SysUtils.TEncoding -> GetBytes
SysUtils.TEncoding -> GetCharCount
SysUtils.TEncoding -> GetCharCount
SysUtils.TEncoding -> GetChars
SysUtils.TEncoding -> GetChars
SysUtils.TEncoding -> GetChars
SysUtils.TEncoding -> GetPreamble
SysUtils.TEncoding -> GetString
SysUtils.TEncoding -> GetString
SysUtils.TMBCSEncoding -> GetPreamble
SysUtils.TUTF8Encoding -> GetPreamble
SysUtils.TUnicodeEncoding -> GetPreamble
SysUtils.TBigEndianUnicodeEncoding -> GetPreamble
Rtti.TRttiMethod -> Invoke
Rtti.TRttiMethod -> Invoke
Rtti.TRttiMethod -> Invoke
Generics.Collections.TList<Rtti.TMethodImplementation.TParamLoc> -> AddRange
Generics.Collections.TList<Rtti.TMethodImplementation.TParamLoc> -> AddRange
Generics.Collections.TList<Rtti.TMethodImplementation.TParamLoc> -> AddRange
Generics.Collections.TList<Rtti.TMethodImplementation.TParamLoc> -> InsertRange
Generics.Collections.TList<Rtti.TMethodImplementation.TParamLoc> -> InsertRange
Generics.Collections.TList<Rtti.TMethodImplementation.TParamLoc> -> InsertRange
Generics.Collections.TArray -> Sort
Generics.Collections.TArray -> Sort
Generics.Collections.TArray -> Sort
Generics.Collections.TArray -> BinarySearch
Generics.Collections.TArray -> BinarySearch
Generics.Collections.TArray -> BinarySearch

Ho scritto questa piccola applicazione per controllare questo comportamento

program ProjectTest;

{$APPTYPE CONSOLE}

uses
  Rtti,
  SysUtils;

Function IsFunction(QualifiedName,MethodName:string):Boolean;
Var
  ctx     : TRttiContext;
  lType   : TRttiType;
  lMethod : TRttiMethod;
Begin
    result:=false;
    ctx := TRttiContext.Create;
    lType:=ctx.FindType(QualifiedName);
    if Assigned(lType) then
    begin
       lMethod:=lType.GetMethod(MethodName);
       try
         if Assigned(lMethod) then
           Result:=(lMethod.ReturnType<>nil);
       except on e : exception do
           Writeln(Format('%s %s -> %s',[e.Message,QualifiedName,MethodName]));
       end;
    end;
End;


var
  ctx     : TRttiContext;
  lType   : TRttiType;
  lMethod : TRttiMethod;
begin
  try
    ctx := TRttiContext.Create;
    for lType in  ctx.GetTypes do
       for lMethod in lType.GetDeclaredMethods do
         IsFunction(lType.QualifiedName,lMethod.Name);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

, che è il modo corretto per determinare se un TRttiMethod è una funzione?

Aggiorna

Grazie ai suggerimenti @Barry, ho riscritto la funzione di evitare l'eccezione, tuttavia questo non risolve il problema di come determinare se un TRttiMethod è una funzione o meno a causa di limitazioni attuali del RTTI.

function IsFunction(lType : TRttiType;MethodName:string):Boolean;
Var
  ctx     : TRttiContext;
  lMethod : TRttiMethod;
Begin
    result:=false;
    if Assigned(lType) then
    begin
       lMethod:=lType.GetMethod(MethodName);
         if Assigned(lMethod) then
           if lMethod.HasExtendedInfo then
            Result:= (lMethod.MethodKind in [mkFunction,mkClassFunction]) //you can be 100 % sure, wich this is a function or not
           else // sorry but the RTTI does not provide this information when the TRttiMethod contains parameters or an resultype that are not supported by the RTTI
             Result:=False;
    end;
End;
È stato utile?

Soluzione

È possibile controllare TRttiMethod.HasExtendedInfo per evitare l'eccezione. La classe genera un'eccezione per tentativi di proprietà di accesso per i quali i dati sono disponibili solo se i rendimenti HasExtendedInfo True.

Si potrebbe prendere in considerazione il controllo della proprietà MethodKind per vedere se è mkFunction o mkClassFunction, ecc, se necessario. restituisce MethodKind mkProcedure se HasExtendedInfo è False.

Altri suggerimenti

La vostra funzione isfunction dovrebbe funzionare bene nella maggior parte dei casi. Ma alcune funzioni non hanno RTTI generato per loro, perché i loro parametri includono i tipi che non hanno RTTI disponibili, come ad esempio TBytes o array of const. Ma per la maggior parte delle funzioni, la funzione funzionerà. Spero solo che le omissioni vengono compilati dal prossima versione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top