繁体   English   中英

Delphi泛型:无法将类和构造函数约束的泛型类型转换为接口

[英]Delphi Generics: cannot cast class and constructor constrained generic type to an interface

procedure Test<TType: class, constructor>;

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

给出以下编译错误:

[DCC错误] TestCNCTypesSerialization.pas(76):E2015运算符不适用于此操作数类型

我不理解为什么。 而且我找不到办法使这项工作...

谢谢!

编译器没有理由相信泛型类型实现了IInterface 您没有将泛型类型限制为从实现IInterface的类IInterface

您可以通过这种方式来限制类,但这可能太过严格了。 或者使用Supports来获取接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM