本文介绍了如何获取自定义属性的属性类型名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi 2007中,我向项目添加了新的字符串类型:

In Delphi 2007, I added a new string type to my project:

type
  String40 = string;

此属性用于一类:

type
  TPerson = class
  private
    FFirstName = String40;
  published
    FirstName: string40 read FFirstName write FFirstName;
  end;

在运行时,我想使用RTTI获取属性FirstName的名称。我希望它是String40:

During runtime, I want to get the name of the property FirstName by using RTTI. I expect it to be String40:

var
  MyPropInfo: TPropInfo;
  PropTypeName: string;
  MyPerson: TPerson;
begin
  MyPerson := TPerson.Create;
  MyPropInfo := GetPropInfo(MyPerson, 'FirstName')^;
  PropTypeName := MyPropInfo.PropType^.Name;

但是,在此示例中,PropTypeName是字符串。我需要怎么做才能获得正确的属性类型名称'String40'?

However, in this example PropTypeName is 'string'. What do I need to do get the correct property type name, 'String40'?

推荐答案

在Delphi5中有效

This works in Delphi5

type
  String40 = type string;

对于其余代码,要使RTTI可用,您应该

As for the rest of your code, to have RTTI available you should


  • 从TPersistent继承TPerson或

  • 对TPerson使用{$ M +}编译器指令

  • 发布Firstname属性

编辑:

program Project1;

uses
  Classes,
  typInfo,
  Dialogs,
  Forms;

{$R *.RES}

type
  String40 = type string;
  TPerson = class(TPersistent)
  private
    FFirstName: String40;
  published
    property FirstName: string40 read FFirstName write FFirstName;
  end;

var
  MyPropInfo: TPropInfo;
  PropTypeName: string;
  MyPerson: TPerson;

begin
  Application.Initialize;
  MyPerson := TPerson.Create;
  MyPropInfo := GetPropInfo(MyPerson, 'FirstName')^;
  PropTypeName := MyPropInfo.PropType^.Name;
  ShowMessage(PropTypeName);
end.

这篇关于如何获取自定义属性的属性类型名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:03