问题描述
下面两行代码之间的区别是什么。两者都试图获取路径,一个正在工作,另一个正在抛出错误。我正在使用Delphi-7
what is the difference between below 2 lines of code. Both are trying to get the path and one is working and other is throwing error. i am working on Delphi-7
Path:= (((FFormOwner as TForm).Designer) as IDesigner).GetPrivateDirectory; --Working
Path:= IDesigner(TForm(FFormOwner).Designer).GetPrivateDirectory ; --Error
下面是使用代码行获取路径的代码。
Below is the code which is using line of code to get the path.
constructor TsampleComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFormOwner:=TForm(Owner);
if not (Owner is TForm) then
repeat
FFormOwner:=TForm(FFormOwner.Owner);
until (FFormOwner is TForm) or (FFormOwner.Owner=nil);
if (csDesigning in ComponentState) then
Path:= (((FFormOwner as TForm).Designer) as IDesigner).GetPrivateDirectory
else
Path:=ExtractFilePath(Application.EXEName);
.
.
end;
推荐答案
IDesigner(TForm(FFormOwner).Designer)
此操作对 Designer执行简单的重新解释
。由于 Designer
的类型为 IDesignerHook
,它与 IDesigner不同,因此失败。 code>。
This performs a simple reinterpretation cast of Designer
. It will fail because Designer
is of type IDesignerHook
which is not the same as IDesigner
.
(FFormOwner as TForm).Designer) as IDesigner
这将对 IDesigner
执行运行时查询,并通过调用<$ c $来解决c> QueryInterface 。这是从现有接口获取不同接口的正确方法。
This performs a runtime query for IDesigner
and is resolved with a call to QueryInterface
. This is the correct way to obtain a different interface from an existing one.
这篇关于TypeCasting:下面两行代码之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!