Delphi Generics:不能将类和构造函数约束泛型类型转换为接口

时间:2015-03-30 17:05:03

标签: delphi delphi-xe2

procedure Test<TType: class, constructor>;

procedure TTestClass.Test<TType>;
var
   Obj1: IInterface;
begin
   Obj1 := TType.Create as IInterface;
end;

给出以下编译错误:

  

[DCC错误] TestCNCTypesSerialization.pas(76):E2015操作员没有   适用于此操作数类型

我不明白为什么。我找不到办法让这项工作......

谢谢!

1 个答案:

答案 0 :(得分:5)

编译器没有理由相信泛型类型实现IInterface。您没有限制从实现IInterface的类派生的泛型类型。

您可以通过这种方式约束班级,但这可能过于严格。或者使用Supports来获取接口。

相关问题