delphi – 基于具体类型的泛型类的条件行为

前端之家收集整理的这篇文章主要介绍了delphi – 基于具体类型的泛型类的条件行为前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于我从 yesterday提出的问题可能还不完全清楚,我没有得到我想要的答案,所以我会尽量用更为一般的方式来制定:

有没有办法根据实例化的泛型类型的实际类型来实现特殊行为,使用Explicit条件语句或使用某种专业化?伪代码

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;

解决方法

您可以通过使用TypeInfo(T)= TypeInfo(string)来回退到RTTI.要测试某些东西是一个类,可以使用像PTypeInfo(TypeInfo(T))^.Kind = tkClass这样的东西.

PTypeInfo类型和tkClass枚举成员在TypInfo单元中定义.

原文链接:https://www.f2er.com/delphi/102733.html

猜你在找的Delphi相关文章