[英]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.