我有一个带有接口的类:
TInterface = interface(IXMLNode)
function Get_One: Boolean;
function Get_Two: Boolean;
function Get_Three: Boolean;
procedure Set_One(Value: Boolean);
procedure Set_Two(Value: Boolean);
procedure Set_Three(Value: Boolean);
property One: Boolean read Get_One write Set_One;
property Two: Boolean read Get_Two write Set_Two;
property Three: Boolean read Get_Three write Set_Three;
end;
TTesting = class(TXMLNode, TInterface)
protected
function Get_One: Boolean;
function Get_Two: Boolean;
function Get_Three: Boolean;
procedure Set_One(Value: Boolean);
procedure Set_Two(Value: Boolean);
procedure Set_Three(Value: Boolean);
end;
并想列出所有属性。我试过这个:
GetMem(PropList, SizeOf(PropList^));
PropCount := GetPropList(TTesting.ClassInfo, tkAny, nil);
GetMem(PropList, PropCount*SizeOf(PPropInfo));
GetPropList(TTesting.ClassInfo, tkAny, PropList);
And PropList
总是空的。当我尝试使用任何形式时并非如此。任何想法 ?
GetPropList()
基于旧式 RTTI,仅描述类属性和类方法,这些属性和类方法声明为published
(你的项目都不是)并且只有当类或祖先(比如TPeristent
),已标记为{$M+}
.
由于您使用的是 XE2,因此可以使用扩展 RTTI(在 Delphi 2010 中引入)来代替。它没有这样的限制。例如:
uses
System.Rtti;
var
Ctx: TRttiContext;
PropList: TArray<TRttiProperty>;
begin
PropList := Ctx.GetType(TTesting).GetProperties;
...
end;
Update:接口是一种特殊情况。接口只允许包含抽象方法,属性只是调用这些方法的语法糖。因此,接口上定义的属性不是真正的属性,就像它们在类类型上一样,因此不会生成 RTTI。这就是为什么您不能枚举从接口继承的属性。您可以使用扩展 RTTI 枚举接口的方法,但前提是接口已标记为{$M+}
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)