Delphi 2010 RTTI - RttiContext.FindType [英] Delphi 2010 RTTI - RttiContext.FindType

查看:370
本文介绍了Delphi 2010 RTTI - RttiContext.FindType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 RttiContext.FindType('Classes.TStringList')我得到TStringList的RttiType没有问题。但是使用 RttiContext.FindType('MyUnit.TMyClass')我总是没有(当然MyUnit在uses子句中)。为什么,有什么问题?

With RttiContext.FindType('Classes.TStringList') I get RttiType of TStringList with no problem. But with RttiContext.FindType('MyUnit.TMyClass') I always get nil (of course MyUnit is in uses clause). Why, what is wrong?

示例:

unit MyUnit; 
interface 
uses 
  Classes; 
type 
  TMyClass = class(TStringList) 
  end; 
implementation 
end. 

Main unit: 
... 
uses 
  MyUnit,
... 
var 
  oCont: TRttiContext; 
  oType: TRttiType; 
begin 
  oCont := TRttiContext.Create; 
  try 
    oType := oCont.FindType('MyUnit.TMyClass'); <== oType = nil !! 
... 


推荐答案

最终的可执行文件中没有包含delphi连接器。快速尝试可以如下:

Probably the class has not included by the delphi linker in the final executable. A fast try can be the following:


  1. 在你的类上声明一个静态方法。这个方法应该是一个空的方法,一个简单的 begin end

  2. 在本机的初始化部分调用此静态方法。

  3. 确保您的项目在某处引用了该单元。

  4. 现在你应该看到具有 TRttiContext.FindType 的课程。

  1. Declare a static method on your class. This method should be an empty method, a simple begin end.
  2. Call this static method in the initialization section of this unit.
  3. Ensure that the unit is referenced in your project somewhere.
  4. Now you should see the class with TRttiContext.FindType.

这篇关于Delphi 2010 RTTI - RttiContext.FindType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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