Delphi 2010 RTTI和接口字段

Delphi 2010 RTTI和接口字段,第1张

概述我对IInterface类型的属性有问题. 我不知道如何使用RTTI为这些属性赋值 这是一个例子: program Project2;uses Forms, RTTI, Windows, TypInfo;{$R *.res}type ITestInterfacedClass = interface ['{25A5B554-667E-4FE4-B932-A5B8D9052A 我对IInterface类型的属性有问题.
我不知道如何使用RTTI为这些属性赋值

这是一个例子:

program Project2;uses  Forms,RTTI,windows,TypInfo;{$R *.res}type  ITestInterfacedClass = interface    ['{25A5B554-667E-4FE4-B932-A5B8D9052A17}']    function GetA: ITestInterfacedClass;    procedure SetA(const Value: ITestInterfacedClass);    property A: ITestInterfacedClass read GetA write SetA;    function GetB: ITestInterfacedClass;    procedure SetB(const Value: ITestInterfacedClass);    property B: ITestInterfacedClass read GetB write SetB;  end;  TTestInterfacedClass = class(TInterfacedobject,ITestInterfacedClass)  private    FA: ITestInterfacedClass;    FB: ITestInterfacedClass;    function GetA: ITestInterfacedClass;    function GetB: ITestInterfacedClass;    procedure SetA(const Value: ITestInterfacedClass);    procedure SetB(const Value: ITestInterfacedClass);  public    property A: ITestInterfacedClass read GetA write SetA;    property B: ITestInterfacedClass read GetB write SetB;  end;  { ITestInterfacedClass }....procedure SetProperty(aleft: TObject {IInterface}; anameProp: string; aRight: IInterface);var  RttiContext: TRttiContext;  RttiType: TRttiType;  RTTIProperty: TRttiProperty;begin  RttiContext := TRttiContext.Create;  RTTIType := RttiContext.GetType(TTestInterfacedClass);  RTTIProperty := RTTIType.GetProperty(anameProp);  if RTTIProperty.PropertyType.TypeKind = tkInterface then    RTTIProperty.SetValue(aleft,TValue.From<IInterface>(aRight));end;var  obj1: TTestInterfacedClass;  intf1,intf2,intf3: ITestInterfacedClass;begin  obj1 := TTestInterfacedClass.Create;  intf1 := obj1;  intf2 := TTestInterfacedClass.Create;  intf3 := TTestInterfacedClass.Create;  intf1.A := intf2;  // intf1.B := intf3;  SetProperty(obj1,'B',intf3);end.

我必须写一个类似的
    intf1.B:= intf3;
要么
    obj1.B = intf3;

使用RTTI.

这可能吗?

UPD
这是工作:

procedure SetProperty(aleft: TObject; anameProp: string; aRight: IInterface);var  RttiContext: TRttiContext;  RttiTypeInterface: TRttiInterfaceType;  RTTIProperty: TRttiProperty;  Value: TValue;begin  RttiContext := TRttiContext.Create;  RTTIType := RttiContext.GetType(aleft.Classtype);  RTTIProperty := RTTIType.GetProperty(anameProp);  if RTTIProperty.PropertyType.TypeKind = tkInterface then  begin    TValue.Make(@aRight,RTTIProperty.PropertyType.Handle,Value);    RTTIProperty.SetValue(aleft,Value);  end;end;
解决方法 不幸的是,这不起作用,因为RTTI.pas中的接口转换代码不调用queryInterface.如果您使用TValue.From< IInterface>放入TValue,则无法将其转换为不同接口类型的TValue,即使接口支持该类型.随意将其提交给QC.

使用TValue.From创建TValue< ITestInterfacedClass>但确实有用.但是你不能使用简单的SetProperty例程.

总结

以上是内存溢出为你收集整理的Delphi 2010 RTTI和接口字段全部内容,希望文章能够帮你解决Delphi 2010 RTTI和接口字段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1273436.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存