delphi - 作为函数结果的属性具有空属性值?

标签 delphi attributes rtti

program Test;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  System.Rtti;

function GetPropertyValue(const AObject: TObject; APropertyName: string): TValue;
var
  oType: TRttiType;
  oProp: TRttiProperty;
begin
  oType := TRttiContext.Create.GetType(AObject.ClassType);
  if oType <> nil then
  begin
    oProp := oType.GetProperty(APropertyName);
    if oProp <> nil then
      Exit(oProp.GetValue(AObject));
  end;
  Result := TValue.Empty;
end;

function GetAttributePropertyValue(const AClass: TClass; AAttribute: TClass;
  AAttributePropertyName: string): TValue;
var
  oAttr: TCustomAttribute;
begin
  for oAttr in TRttiContext.Create.GetType(AClass).GetAttributes do
    if oAttr.InheritsFrom(AAttribute) then
      Exit(GetPropertyValue(oAttr, AAttributePropertyName));
  Result := nil;
end;

function GetClassAttribute(const AClass: TClass; AAttribute: TClass): TCustomAttribute;
begin
  for Result in TRttiContext.Create.GetType(AClass).GetAttributes do
    if Result.InheritsFrom(AAttribute) then
      Exit;
  Result := nil;
end;

type
  DescriptionAttribute = class(TCustomAttribute)
  private
    FDescription: string;
  public
    constructor Create(const ADescription: string);
    property Description: string read FDescription;
  end;

constructor DescriptionAttribute.Create(const ADescription: string);
begin
  FDescription := ADescription;
end;

type
  [Description('MyClass description')]
  TMyClass = class(TObject);

var
  oAttr: TCustomAttribute;
begin
  { ok, output is 'MyClass description' }
  WriteLn(GetAttributePropertyValue(TMyClass, DescriptionAttribute, 'Description').AsString);
  { not ok, output is '' }
  oAttr := GetClassAttribute(TMyClass, DescriptionAttribute);
  WriteLn(DescriptionAttribute(oAttr).Description);
  // WriteLn(oAttr.ClassName); // = 'DescriptionAttribute'
  ReadLn;
end.

我需要 rtti 属性。我希望使用函数 GetClassAttribute() 获取属性,但结果不是预期的。

函数GetAttributePropertyValue()的结果是正确的(第一个WriteLn),但函数GetClassAttribute()的结果是具有空描述值的DescriptionAttribute。为什么?

获取属性作为函数结果的正确方法是什么?

TIA 致以最诚挚的问候 布兰科

最佳答案

问题是,如果 TRttiContext 超出范围,则在查询信息(包括属性)期间创建的所有 RTTI 相关对象都将被销毁。

当您在属性类上放置析构函数时,您可以验证这一点。

最近版本在 TRttiContext 上引入了 KeepContextDropContext 方法,您可以使用或只是将全局变量放在某处,并使其触发内部通过调用Create或其他方式创建。我通常将 TRttiContext 变量作为类变量放入使用 RTTI 的类中。

KeepContextDropContext 可以在您可能没有一个全局 TRttiContext 实例来确保其生命周期的代码中使用,因为您正在使用其他实例具有自己的 TRttiContext 引用的类、方法和例程 - 例如,请参阅它在 System.Classes 中的使用,其中 BeginGlobalLoading 期间 KeepContext 正在被调用,并且位于 EndGlobalLoading DropContext 中。

关于delphi - 作为函数结果的属性具有空属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48445227/

相关文章:

string - 尝试从字符串中删除某个字符,直到该字符串仅包含x个字符

c# - 基于属性对枚举执行 LINQ 操作

python - 有什么方法可以将变量传递给 django 元类?

delphi - Variant 内容类型的字符串表示形式?

delphi - 如何计算出 TListBox.ScrollWidth 使用哪些值?

delphi - 如何清除jpeg图像?

delphi - TRemotable 派生类中的 Index 选项有何作用?

image - Magento:将图像(或任何属性)添加到自定义模块

c++ - 在 C++ 中创建类型 vector

c++ - RTTI 是如何工作的?