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

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

问题描述

我听说了很多关于新的/改进的 Delphi 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 或其他托管语言中的反射那样功能齐全,因为它在编译后的代码,而不是中间语言(字节码).然而,这是一个非常相似的概念,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.

看起来有人甚至试图实现 Spring (DI 框架),适用于 Delphi 2010.

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

所以这绝对是非常有用的,虽然我不确定你能否向 PHB 解释它;它的大部分用处可能会通过 3rd-party 库和框架来实现,这与它今天在 .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.

我回答问题了吗?

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

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