我需要确定TRttiMethod
是否为函数
到目前为止,我写了这个函数
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;
但此功能因此异常而失败
我测试以下内容时Insufficient RTTI available to support this operation.
IsFunction('SysUtils.Exception','CreateFmt')
也失败了这些类和方法
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
我写了这个小应用来检查这种行为
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.
这是确定TRttiMethod
是否为函数的正确方法吗?
更新
由于@Barry建议,我重写了该函数以避免异常,但是这并没有解决由于RTTI的当前限制如何确定TRttiMethod
是否为函数的问题。
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;
答案 0 :(得分:9)
您可以查看TRttiMethod.HasExtendedInfo
以避免异常。如果HasExtendedInfo
返回True
,则该类会尝试访问仅对数据可用的属性时抛出异常。
您可以根据需要考虑检查MethodKind
属性,看看它是mkFunction
还是mkClassFunction
等。如果MethodKind
为mkProcedure
,则HasExtendedInfo
会返回False
。
答案 1 :(得分:0)
在大多数情况下,您的IsFunction功能应该可以正常工作。但是有些函数没有为它们生成RTTI,因为它们的参数包括没有RTTI可用的类型,例如TBytes
或array of const
。但对于大多数功能,您的功能将起作用。 I just hope that the omissions get filled in by the next version.