鸭子在 Delphi 2007 中打字? [英] Duck typing in Delphi 2007?
本文介绍了鸭子在 Delphi 2007 中打字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:
有没有办法用 Delphi 2007 做鸭子输入(即没有泛型和高级 Rtti 功能)?
Is there a way to do duck typing with Delphi 2007 (i.e. without generics and advanced Rtti features)?
Delphi 2010 以后的鸭子打字资源:
- Duck Duck Delphi in google project by ARCANA.
- Duck Typing in Delphi by Daniele Teti.
- AOP and duck typing in Delphi by Stefan Glienke.
上次
我深入研究了上面列出的资源,并研究了这里发布的每个答案.
I've delved more into the resouces listed above and studied every posted answers here.
我最终完善了我的要求并做了一个后续帖子这个问题.
I end up refining my requirement a made a follow up post to this question.
推荐答案
在 ObjAuto.pas 和可调用的变体类型的帮助下,这应该是可能的(用 XE 编写,但也应该在 Delphi 7 或更低版本中运行):>
With the help of the ObjAuto.pas and invokable variant types it should be possible (written in XE but should also run in Delphi 7 or lower):
unit DuckTyping;
interface
function Duck(Instance: TObject): Variant;
implementation
uses
ObjAuto,
SysUtils,
TypInfo,
Variants;
type
TDuckVarData = packed record
VType: TVarType;
Reserved1, Reserved2, Reserved3: Word;
VDuck: TObject;
Reserved4: LongWord;
end;
TDuckVariantType = class(TPublishableVariantType)
protected
function GetInstance(const V: TVarData): TObject; override;
public
procedure Clear(var V: TVarData); override;
procedure Copy(var Dest: TVarData; const Source: TVarData;
const Indirect: Boolean); override;
function DoFunction(var Dest: TVarData; const V: TVarData;
const Name: string; const Arguments: TVarDataArray): Boolean; override;
end;
var
DuckVariantType: TDuckVariantType;
{ TDuckVariantType }
procedure TDuckVariantType.Clear(var V: TVarData);
begin
V.VType := varEmpty;
TDuckVarData(V).VDuck := nil;
end;
procedure TDuckVariantType.Copy(var Dest: TVarData; const Source: TVarData;
const Indirect: Boolean);
begin
if Indirect and VarDataIsByRef(Source) then
VarDataCopyNoInd(Dest, Source)
else
begin
with TDuckVarData(Dest) do
begin
VType := VarType;
VDuck := TDuckVarData(Source).VDuck;
end;
end;
end;
function TDuckVariantType.DoFunction(var Dest: TVarData; const V: TVarData;
const Name: string; const Arguments: TVarDataArray): Boolean;
var
instance: TObject;
methodInfo: PMethodInfoHeader;
paramIndexes: array of Integer;
params: array of Variant;
i: Integer;
ReturnValue: Variant;
begin
instance := GetInstance(V);
methodInfo := GetMethodInfo(instance, ShortString(Name));
Result := Assigned(methodInfo);
if Result then
begin
SetLength(paramIndexes, Length(Arguments));
SetLength(params, Length(Arguments));
for i := Low(Arguments) to High(Arguments) do
begin
paramIndexes[i] := i + 1;
params[i] := Variant(Arguments[i]);
end;
ReturnValue := ObjectInvoke(instance, methodInfo, paramIndexes, params);
if not VarIsEmpty(ReturnValue) then
VarCopy(Variant(Dest), ReturnValue);
end
else
begin
VarClear(Variant(Dest));
end;
end;
function TDuckVariantType.GetInstance(const V: TVarData): TObject;
begin
Result := TDuckVarData(V).VDuck;
end;
function Duck(Instance: TObject): Variant;
begin
TDuckVarData(Result).VType := DuckVariantType.VarType;
TDuckVarData(Result).VDuck := Instance;
end;
initialization
DuckVariantType := TDuckVariantType.Create;
finalization
FreeAndNil(DuckVariantType);
end.
你可以像这样简单地使用它:
You can simply use it like this:
type
{$METHODINFO ON}
TDuck = class
public // works in XE, not sure if it needs to be published in older versions
procedure Quack;
end;
procedure TDuck.Quack;
begin
ShowMessage('Quack');
end;
procedure DoSomething(D: Variant);
begin
D.Quack;
end;
var
d: TDuck;
begin
d := TDuck.Create;
try
DoSomething(Duck(d));
finally
d.Free;
end;
end;
这篇关于鸭子在 Delphi 2007 中打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!