I am using Delphi 10.1 Berlin update 2. Following is the code that reproduces the problem:
uses
System.SysUtils,
Rtti;
type
TEnum = (t1, t2);
TIndexedEnum = (to1=1, to2);
TClass1 = class
constructor Create(pEnum: TEnum);
end;
TClass2 = class
constructor Create(pEnum: TIndexedEnum);
end;
constructor TClass1.Create(pEnum: TEnum);
begin
end;
constructor TClass2.Create(pEnum: TIndexedEnum);
begin
end;
function MethodParamCount(pMethodName: String; pClass: TClass): Integer;
var
rContext: TRttiContext;
rType: TRttiType;
FMethods: TArray<TRttiMethod>;
I: Integer;
begin
rContext := TRttiContext.Create;
rType := rContext.GetType(pClass);
FMethods := rType.GetMethods;
for I := Low(FMethods) to High(FMethods) do
if SameText(pMethodName, FMethods[I].Name) then
begin
Result := Length(FMethods[I].GetParameters);
Exit;
end;
Result := -1;
end;
begin
WriteLn(IntToStr(MethodParamCount('Create', TClass1))); // Prints 1
WriteLn(IntToStr(MethodParamCount('Create', TClass2))); // Prints 0
ReadLn;
end.
Is that a bug of the RTTI implementation, or I am missing something? Is there a way to circumvent this issue without changing the class implementation?
Edit: I want to find a constructor without any parameters so I can call it using Invoke()
. The thing is, RTTI is telling me there are no parameters for a method that actually has a parameter.