在哪里德尔福属性真实的例子? [英] Where are the Delphi Attributes Real World Examples?

查看:137
本文介绍了在哪里德尔福属性真实的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过 TMS奥勒留的,我们可以用新2010属性功能来序列数据库表字段为在运行时对象的属性,例如,我不是面向架构此深对象上的专家,所以我期待到TMS源$ C ​​$ c和无法理解如何实现它自己,不是为DB,不为XML。

I know by TMS Aurelius that we can use the "new" 2010 attributes feature to serialize database table fields into object properties at run-time, for example, and I am not an expert on this deep object oriented schema, so I look into the TMS source code and could not understand how to implement it myself, not for DB, not for XML.

所以,我找了所有谷歌的结果对德尔福属性和所有的人后是声明的例子,然后甚至呈现他们在行动的例子之前停止。

So I've looked for all Google's results on Delphi Attributes and all that people post are declaration examples and then stops before even showing their examples in action.

又在哪里是我们如何能够预测真实世界的例子,声明,code和使用窗体里面的果汁类/执行code?

Then where are the real world examples of how can we project, declare, code and USE those juiced classes inside a form/executing code?

没有任何人有一个例子,在这里分享或知道一个好的文章是完整的?

Does anyone have an example to share here or know a good article that is complete?

EDIT1:

答案应该有一个 TForm的 TButton的其中,点击后,执行一些使用属性类创造,不回答只显示属性和类接口,因为有许多这样的例子声明,因为我之前

The answer should have a TForm with a TButton where, when clicked, execute some use of the attribute classes created, do not answer showing just the attribute and classes interfaces, because there are many of those declaration examples as I told before

推荐答案

如果您想声明自己你的属性,你可以做这样的:

If you want to declare you own attribute, you can do it like this:

type
  TDisplayLabelAttribute = class(TCustomAttribute)
  private
    FText: string;
  public
    constructor Create(const aText: string);
    property Text: string read FText write FText;
  end;

属性是普通班,有在 TCustomAttribute 作为它的祖先。你实现它像往常一样:

An attribute is a regular class, that has the TCustomAttribute as its ancestor. You implement it as usual:

implementation

constructor TDisplayLabelAttribute.Create(const aText: string);
begin
  FText := aText;
end;

现在属性声明并实现,你可以使用它:

Now the attribute is declared and implemented, you can just use it:

[DisplayLabel('My Class')]
TMyClass = class
end;

所以,现在你有一个属性声明和实现,你用它来显示标签添加到某个类。最后一个阶段是使用该属性,因为你有一个类的装修的吧。使用该属性不驻留在属性也不装饰类code,它在将使用装饰服务层实现。

So now you have an attribute declared and implemented and you have used it to add a display label to some class. The final phase is to use that attribute, since you have a class decorated with it. The code that uses the attribute does not resides in the attribute nor the decorated class, it is implemented in the service layer that will use the decoration.

让我们说我们有一个返回可能显示标签一类的类:

Let's say we have a class that returns a possible display label for a class:

type
  TArtifactInspector = class
  public
    class function DisplayLabelFor(aClass: TClass): string;
  end;

这方法将查看一个类,并返回其显示的标签,因为它的存在。否则,它返回一个空字符串:

That method will inspect a class and return its display label, given it exists. Otherwise it returns an empty string:

implementation

uses
  Rtti;

class function TArtifactInspector.DisplayLabelFor(aClass: TClass): string;
var
  rttiContext: TRttiContext;
  rttiType: TRttiType;
  attribute: TCustomAttribute;
begin
  rttiContext := TRttiContext.Create;
  try
    rttiType := rttiContext.GetType(aClass);
    for attribute in rttiType.GetAttributes do
      if attribute is TDisplayLabelAttribute then
        Exit(TDisplayLabelAttribute(attribute).Text);
    Result := '';
  finally
    rttiContext.Free;
  end; // try to recover and return the DisplayLabel
end;

这篇关于在哪里德尔福属性真实的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆