我有不受约束的泛型类型 Atomic,它实现了一个初始化程序(详细信息在我的 previous question 中)。
type
Atomic<T> = class
type TFactory = reference to function: T;
class function Initialize(var storage: T; factory: TFactory): T;
end;
现在我想编写简化的 Initialize 函数,该函数将从 T 获取类型信息(假设 typeof(T) 是 tkClass)并使用默认构造函数创建新实例(必要时)。
遗憾的是,这失败了:
class function Atomic<T>.Initialize(var storage: T): T;
begin
if not assigned(PPointer(@storage)^) then begin
if PTypeInfo(TypeInfo(T))^.Kind <> tkClass then
raise Exception.Create('Atomic<T>.Initialize: Unsupported type');
Result := Atomic<T>.Initialize(storage,
function: T
begin
Result := TClass(T).Create; // <-- E2571
end);
end;
end;
编译器报告错误E2571类型参数“T”没有类或接口(interface)约束
。
如何欺骗编译器创建类 T 的实例?
最佳答案
您可以使用GetTypeData
获取类引用:
Result := T(GetTypeData(PTypeInfo(TypeInfo(T)))^.ClassType.Create);
在 Delphi XE2 中(希望在下一个版本中),您可以执行以下操作:
var
xInValue, xOutValue: TValue;
xInValue := GetTypeData(PTypeInfo(TypeInfo(T)))^.ClassType.Create;
xInValue.TryCast(TypeInfo(T), xOutValue);
Result := xOutValue.AsType<T>;
(这种相当规避的方法是由 OmniThreadLibrary 论坛中使用的 cjsalamon
发现的: Error in OtlSync XE2 。)
关于delphi - 基于无约束泛型类型创建对象实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8355456/