- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近从 D2010 迁移到 DXE2,并在 XE2 和 XE3(在我的 friend XE3 中测试)中发现了一个与类内 TBytes 字段的 RTTI 生成相关的严重错误(或功能?)。
我发现类中 TBytes 变量的 RTTI 信息永远不会生成。
下面的代码在D2010中运行良好,但在XE2/XE3中显示“错误”消息
有人知道吗?这将彻底破坏我们所有的软件数据序列化实现
要测试代码,请将 Rtti 单位添加到使用声明中
type
TMyClass = class
public
Field1: Integer;
Field2: TBytes;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
i: Integer;
Data: TMyClass;
Rtti: TRttiContext;
RttiClassType: TRttiInstanceType;
begin
Data := TMyClass.Create;
try
// Get the context
Rtti := TRttiContext.Create;
try
// Get the type for the class
RttiClassType := TRttiInstanceType(Rtti.GetType(Data.ClassInfo));
// Check the fields
for i := 0 to High(RttiClassType.GetFields) do
begin
// Check the field type
if not Assigned(RttiClassType.GetFields[i].FieldType) then
ShowMessage('Error');
end;
finally
Rtti.Free;
end;
finally
Data.Free;
end;
end;
检查 Field2 是否为 TBytes 时,将显示错误消息,因为 FieldType 始终为 nil!!!
有谁知道 XE2 与 D2010 相比 RTTI 发生了什么变化?也许是因为 TBytes 类型从 Byte 数组更改为通用数组?
最佳答案
您可以修复此错误(它实际上与 Mason 提到的错误不同)。
type
FixTypeInfoAttribute = class(TCustomAttribute)
public
FTypeInfo: PPTypeInfo;
constructor Create(TypeInfo: PTypeInfo);
end;
procedure FixFieldType(TypeInfo: PTypeInfo);
var
ctx: TRttiContext;
t: TRttiType;
f: TRttiField;
a: TCustomAttribute;
n: Cardinal;
begin
t := ctx.GetType(TypeInfo);
for f in t.GetFields do
begin
for a in f.GetAttributes do
begin
if (a is FixTypeInfoAttribute) and f.ClassNameIs('TRttiInstanceFieldEx') then
begin
WriteProcessMemory(GetCurrentProcess, @PFieldExEntry(f.Handle).TypeRef,
@FixTypeInfoAttribute(a).FTypeInfo, SizeOf(Pointer), n);
end;
end;
end;
end;
constructor FixTypeInfoAttribute.Create(TypeInfo: PTypeInfo);
begin
FTypeInfo := PPTypeInfo(PByte(TypeInfo) - SizeOf(Pointer));
end;
然后将属性添加到类定义中:
type
TMyClass = class
private
Field1: Integer;
[FixTypeInfo(TypeInfo(TBytes))]
Field2: TBytes;
end;
并确保调用 FixFieldType 例程:
initialization
FixFieldType(TypeInfo(TMyClass));
在 XE 上测试
关于Delphi XE2 RTTI坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679240/
我需要能够从非 RTTI 代码派生一个类到 RTTI 代码项目中。我正在使用 LLVM,它在代码中禁用了 RTTI。我知道我不能对他们的代码执行 typeinfo 和 dynamic_cast,这没关
我有一个解决方案来构建一个在其项目属性中启用运行时类型信息的 DLL。这是 Visual Studio 2005 中的默认值 (/GR)。在我们的库中,我们有很多 dynamic_cast,因此我目前
我正在尝试使用 boost 序列化来序列化具有 Plain-old-Data 类型的简单类。但是,我唯一的要求是不能使用 RTTI。因此,我正在使用 gcc 4.4.1 for ARM Linux 和
我在我的测试应用程序中使用了一些 LLVM 静态库和 boost::program_options。问题是使用 LLVM 库的源代码应该用 -fno-rtti 编译(llvm-config -cxxf
我的功能模块在运行时接收一个表名和一个列名。 我想获取列的长度:透明表中允许有多少个字符? 我用我最喜欢的搜索引擎找到了RTTS . 但是文档中的示例将变量传递给 RTTS 方法 DESCRIBE_B
我想知道如何在 C++Builder 2010 中使用 TRttiMethod 类的 Invoke 方法。 这是我的代码 Tpp *instance=new Tpp(this); TValue *ar
假设我有: type TClassA = class function prova: integer; virtual; function provaSuA: integer; virtua
我尝试使用 Delphi 2010 编写一种对象/记录序列化程序,并想知道是否有一种方法可以检测记录是否是变体记录。例如。 Types.pas 中定义的 TRect 记录: TRect = recor
我有这样的类(class) TuserClass = class private FUtilisateurCode: string; FUtilisateurCle: string; public
给定一个包含类型名称的文本字符串,是否有某种方法可以获取适当的类型本身? 我想做这样的事情: type TSomeType = class // yadda yadda end; pr
不允许使用此代码: class constructor TOmniMultiPipelineStage.Create; var RTTIType: TRttiType; begin RTTIT
我在使用 rtti 获取有关泛型类型的类字段的信息时遇到问题。经过一番谷歌搜索后,我在 QC 中找到了一个条目描述问题。我的问题是,是否有人知道解决方法,或者是否已修复 Delphi XE2。以下是
正如Rtti data manipulation and consistency in Delphi 2010中已经讨论的那样可以通过使用一对 TRttiField 和实例指针访问成员来达到原始数据和
我想使用 RTTI 在设计时而不是运行时检查项目源文件中包含的类型。 据我所知,这是不受支持的,但 this question 评论中的讨论表明这是可能的,并且已经用于多个 Delphi 版本。这是我
如何在 delphi (XE4) 中通过 Rtti 释放对象的字段? 我需要动态释放所有字段 我可以找到字段,但我现在不知道如何释放它们: destructor TKnBase.Destroy; va
我制作了一个非常通用的单元,其中的设置对象都是 TObject,我不想使用任何单元,这就是我这样做的原因。所以我的做法是使用RTTI来调用一切。但现在我面临一个问题,我可以调用所有函数并给出参数和所有
RTTI 被认为是糟糕设计的原因是什么? Stroustrup 在他的书 TC++PL 中写道,最常见的使用 RTTI 技术的情况是使用 switch 指令,当人们想要根据传递对象的“真实”类型来决定
我看到了一些关于避免 RTTI 的问题,但我的问题似乎更具体一些。这是一个示例案例: struct Base {}; struct A : Base {}; struct B : Base {}; s
我一直在寻找一种方法来解决动态转换类型检查的缓慢问题。在你开始说我应该重新设计一切之前,让我告诉你,设计是 5 年前决定的。我无法修复之后的所有 400,000 行代码(我希望我可以),但我可以进行一
我在 MS Visual Studio Express 2012 中编写了这段代码以查看 rtti 行为。 但它没有按预期工作。 我的代码有什么问题? Shape.h class Shape { pu
我是一名优秀的程序员,十分优秀!