我有一个类实例和两个引用它.有一个对象reference(aMII : TMyInterfaceImpl)和一个接口引用(iMI : IMyInterface).当我使用对象引用将其方法之一分配给方法类型变量(methodRef : TMyMethodRef)时,它工作正常.
但是如果我想在相同的情况下使用接口引用,编译器会抛出一个错误:
[dcc32错误] Unit1.pas(66):E2010不兼容的类型:'TMyMethodRef'和'过程,无类型指针或无类型参数'
最小的例子:
type
TMyMethodRef = procedure of object;
//TMyMethodRef = procedure of interface; // Invalid definition, just a desperate attempt
IMyInterface = interface
['{BEA60A2B-C20F-4E02-A938-65DD4332ADB0}']
procedure foo;
end;
TMyInterfaceImpl = class ( TInterfacedObject, IMyInterface )
procedure foo;
end;
procedure TMyInterfaceImpl.foo;
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aMII : TMyInterfaceImpl;
iMI : IMyInterface;
methodRef : TMyMethodRef;
begin
aMII := TMyInterfaceImpl.Create;
iMI := aMII;
methodRef := aMII.foo; // …Run Code Online (Sandbox Code Playgroud) 我想为通用接口定义一个列表类型.存储泛型类型数据的树实现需要它.不幸的是,这个简单的解决方案不起作用:
uses
Generics.Collections;
type
ITreeNode<T> = interface;
TTreeNodeList<T> = TList<ITreeNode<T>>;
ITreeNode<T> = interface
['{BC384FDB-4509-44D3-8946-E7ECD4417C4D}']
//...
function getChildNodes : TTreeNodeList<T>;
function getData : T;
end;
TTreeNode<T> = class ( TInterfacedObject, ITreeNode<T> )
//...
end;
procedure foo;
var
node : ITreeNode<cardinal>;
begin
node := TTreeNode<cardinal>.create;
//...
end;
Run Code Online (Sandbox Code Playgroud)
有没有诀窍来实现它?
我使用德尔福10.3。这是获取对应实例变量的一种例程TRTTIType。但是有什么方法可以填补此处标记为“缺少代码”的空白:
function getGenericTypeName<T> : string;
var
ctx : TRTTIContext;
aRT : TRTTIType;
begin
ctx := TRTTIContext.Create;
try
aRT := *** missing code for T *** // Get the TRTTIType for type T
result := aRT.Name;
finally
ctx.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud) CapsLock无意中按下时非常烦人,我意识到我输入的文本是相反的情况.是否有任何热键可以反转Delphi 10.2编辑器中所选文本的大小写?也许对于其他情况(上层,下层,资本,骆驼,uncapitalCamel).
MartynA回答了这个问题,我必须意识到映射到这个函数(Ctrl-O U)的热键不是一个微不足道的.所以我想到的另一个问题是:有没有办法自定义热键映射?正如我所看到的"选项"对话框不可用.(或者我找不到它)
当我传递一个 proc 引用作为参数并希望将其分配给另一个 proc 引用变量(TMyRec.proc)时,也许它想调用该 proc 并分配结果...结果是 GPF。如何将传递 proc ref 的参数分配给另一个 proc ref?
示例:它只是围绕作业进行。没有分配为进程的实际任务。
单元5.pas:
unit Unit5;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm5 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
type
TMyProc = reference to procedure ( i_ : integer );
PMyRec = ^TMyRec;
TMyRec = packed record
i : integer;
proc : TMyProc;
end; …Run Code Online (Sandbox Code Playgroud)