jpf*_*ius 15 delphi generics types delphi-2009
由于我昨天提出的问题可能并不完全清楚,而且我没有得到我想要的答案,我将尝试以更一般的方式制定它:
有没有办法基于实例化的泛型类型的实际类型实现特殊行为,使用明确的条件语句或使用某种特化?伪代码:
TGenericType <T> = class
function Func : Integer;
end;
...
function TGenericType <T>.Func : Integer;
begin
if (T = String) then Exit (0);
if (T is class) then Exit (1);
end;
...
function TGenericType <T : class>.Func : Integer;
begin
Result := 1;
end;
function TGenericType <String>.Func : Integer;
begin
Result := 0;
end;
Run Code Online (Sandbox Code Playgroud)
Bar*_*lly 22
您可以通过使用来回退到RTTI TypeInfo(T) = TypeInfo(string).要测试某事是否是一个类,你可以使用类似的东西PTypeInfo(TypeInfo(T))^.Kind = tkClass.
的PTypeInfo类型和tkClass枚举构件在所定义的TypInfo单元.