问题描述
我在寻找扫描所有加载的类包含自定义属性,如果可能的话类的方式,不使用的RegisterClass()。
I'm looking for a way of scanning all loaded classes for classes which contain a custom attribute, if possible, without using RegisterClass().
推荐答案
首先,你必须创建 TRttiContext
,然后用得到所有加载的类 getTypes
。后,您可以通过 TypeKind = tkClass
过滤器类型;
下一步是枚举属性,并检查是否有您的属性;
at first you have to create TRttiContext
, then get all loaded classes using getTypes
. after that you can filter types by TypeKind = tkClass
;next step is to enumerate attributes and check if it has your attribute;
属性和测试类delcaration:
attribute and test-class delcaration:
unit Unit3;
interface
type
TMyAttribute = class(TCustomAttribute)
end;
[TMyAttribute]
TTest = class(TObject)
end;
implementation
initialization
TTest.Create().Free(); //if class is not actually used it will not be compiled
end.
,然后找到它:
program Project3;
{$APPTYPE CONSOLE}
uses
SysUtils, rtti, typinfo, unit3;
type TMyAttribute = class(TCustomAttribute)
end;
var ctx : TRttiContext;
t : TRttiType;
attr : TCustomAttribute;
begin
ctx := TRttiContext.Create();
try
for t in ctx.GetTypes() do begin
if t.TypeKind <> tkClass then continue;
for attr in t.GetAttributes() do begin
if attr is TMyAttribute then begin
writeln(t.QualifiedName);
break;
end;
end;
end;
finally
ctx.Free();
readln;
end;
end.
输出 Unit3.TTest
通话的RegisterClass注册一类的流媒体系统....一旦类被注册,就可以装载或组件流媒体系统保存。
所以,如果你不需要的组件流的(只要找到一些属性类),没有必要的RegisterClass
so if you don't need component streaming (just find classes with some attribute), there is no need to RegisterClass
这篇关于扫描所有类对于给定的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!