为什么要在Delphi中关心RTTI? [英] Why should I care about RTTI in Delphi?

查看:185
本文介绍了为什么要在Delphi中关心RTTI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听过很多关于新的/改进的德尔福2010的RTTI功能,但我必须承认我的无知...我不明白。我知道每个版本的Delphi都支持RTTI ...我知道RTTI(运行时类型信息)允许我在应用程序运行时访问类型信息。

I've heard a lot about the new/improved RTTI capabilities of Delphi 2010, but I must admit my ignorance...I don't understand it. I know every version of Delphi has supported RTTI...and I know that RTTI (Runtime Type Information) allows me to access type information while my application is running.

但是是什么意思? Delphi 2010的RTTI支持与在.NET中的反思相同

But what exactly does that mean? Is Delphi 2010's RTTI support the same thing as reflection in .NET?

有人可以解释为什么RTTI有用吗?假装我是你的头发老大,帮我理解为什么RTTI很酷。如何在现实世界的应用程序中使用它?

Could someone please explain why RTTI is useful? Pretend I'm your pointy haired boss and help me understand why RTTI is cool. How might I use it in a real-world application?

推荐答案

在Delphi中的RTTI仍然不是 >作为.NET或其他托管语言中的Reflection的全功能,因为它在编译代码上运行,而不是中间语言(字节码)。然而,这是一个非常相似的概念,Delphi 2010中的新的RTTI系统使它更接近于反思,揭露了一个整个面向对象的API。

RTTI in Delphi is still not quite as full-featured as Reflection in .NET or other managed languages, because it is operating on compiled code, not an Intermediate Language (bytecode). However, it is a very similar concept, and the new RTTI system in Delphi 2010 brings it a lot closer to reflection, exposing an entire object-oriented API.

D2010之前,RTTI相当有限。关于我记得做的唯一的事情是转换一个枚举类型到字符串(或反之亦然)用于下拉列表。我可能有一次使用它来控制持久性

Pre-D2010, the RTTI was pretty limited. About the only thing I ever remember doing with it was converting an enumerated type to a string (or vice versa) for use in drop-down lists. I may have used it at one point for control persistence.

使用D2010中的新RTTI,您可以做更多的事情:

With the new RTTI in D2010 you can do a lot more things:

属性 - 基于元数据( TCustomAttribute )。典型的用例将是自动验证属性和自动权限检查,您通常需要编写许多代码,两件事情。

Attribute-based metadata (TCustomAttribute). Typical use cases would be automatic validation of properties and automated permission checks, two things that you normally have to write a lot of code for.

添加活动脚本支持(即使用Windows脚本控件)

Adding Active Scripting support (i.e. using the Windows script control)

构建插件系统;你可以这样做,但是有很多头痛。我无法从上到下找到一个非常好的例子,但所有必要的功能现在都可以使用。

Building a plug-in system; you could do this before, but there were a lot of headaches. I wasn't able to find a really good example of someone doing this from top to bottom, but all of the necessary functions are available now.

看起来就像有人甚至试图为Delphi 2010实施 Spring (DI框架)。

It looks like someone's even trying to implement Spring (DI framework) for Delphi 2010.

所以这绝对是非常有用的,虽然我不知道你能够如何解释它PHB;它的大部分用途可能会通过第三方库和框架来实现,就像现在在.NET社区中一样的工作方式一样 - 很少看到反思代码位于业务逻辑中,但一个典型的应用程序将会使用几个基于反射的组件,如对象关系映射器或IoC容器。

So it's definitely very useful, although I'm not sure how well you'd be able to explain it to a PHB; most of its usefulness is probably going to be realized through 3rd-party libraries and frameworks, much the same way it works in the .NET community today - it's rare to see reflection code sitting in the business logic, but a typical app will make use of several reflection-based components like an Object-Relational Mapper or IoC Container.

我是否回答了这个问题?

Have I answered the question?

这篇关于为什么要在Delphi中关心RTTI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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