0

I am using Delphi 2010

I get the error: E2506 Method of parameterized type declared in interface section must not use local symbol.

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.

4

1 回答 1

2

这是 Delphi 2010 中众多泛型错误之一。您的代码在 XE2 中编译。您的选择是寻找适用于 2010 年的解决方法,或者进行升级。Delphi XE 和 XE2 确实包含大量针对泛型编译器错误的修复,因此如果您认真使用泛型,Delphi 2010 不是一个好选择。

于 2012-08-03T10:26:00.687 回答