[英]Error passing a generic type from a class to another
I am using Delphi 2010 我正在使用Delphi 2010
I get the error: E2506 Method of parameterized type declared in interface section must not use local symbol. 我收到错误:E2506接口部分声明的参数化类型的方法不能使用本地符号。
Is there a way to accomplish this task? 有没有办法完成这项任务?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Rtti;
type
MyFormType<T: TForm> = class
class procedure SpecialOpen(var FormVar: T; Params: array of TValue);
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ShowForm<T1: TForm>(var aForm: T1);
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.ShowForm<T1>(var aForm: T1);
begin
if aForm = nil then
MyFormType<T1>.SpecialOpen(aForm, [Self]) // <-- Error
else
aForm.Show;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowForm<TForm2>(Form2)
end;
{ MyFormType<T> }
class procedure MyFormType<T>.SpecialOpen(var FormVar: T; Params: array of TValue);
var lRttiContext: TRttiContext;
begin
FormVar := lRttiContext.GetType(TClass(T)).GetMethod('Create').Invoke(TClass(T), Params).AsType<T>;
FormVar.Show;
end;
end.
Tanks and sorry for my english. 坦克,对不起我的英语。
This is one of a great many generics bugs in Delphi 2010. Your code compiles in XE2. 这是Delphi 2010中许多泛型错误之一。您的代码在XE2中编译。 Your options are to look for a workaround that works in 2010, or to upgrade.
您可以选择在2010年运行的解决方法,或升级。 Delphi XE and XE2 do include a great many fixes for generics compiler bugs and so if you are serious about making use of generics, Delphi 2010 is not a great choice.
Delphi XE和XE2确实包含了许多针对泛型编译器错误的修复,因此如果您认真考虑使用泛型,Delphi 2010不是一个很好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.