如何获取访问该 Variant 的 Delphi 6 索引属性的基础原始 Variant 值?

发布于 2024-12-29 18:59:41 字数 815 浏览 1 评论 0原文

我有一个 Delphi 6 类对象,其中包含 30 个变体的数组,每个变体都通过不同的索引属性公开。例如:

property responseCode: integer
        Index 7 read getIndexedProperty_integer write setIndexedProperty_integer;

我这样做是为了更轻松地使用变体数组(帮助 IDE 自动完成)并提供类型安全。它工作得很好,但现在我有一个皱纹。当构造包装它的类时,Variants 数组被初始化为 NULL,因此我可以判断是否曾经使用值实例化过特定变体。这样做的结果是,如果仅实例化某些变体(给定有效值),则当 Delphi 尝试将变体转换为由索引属性。

我宁愿不为每个索引属性声明“isValid”属性。我想知道是否有一种方法可以使用 TypeInfo 库来获取底层 Variant 的原始值,而不必直接访问索引属性,从而触发转换异常。然后我可以编写类似的代码(使用上面的示例属性):

isValidProperty(responseCode);

如果responseCode 属性底层的 Variant 不为 NULL,则该函数将返回 TRUE,如果为 NULL,则该函数将返回 FALSE。

我知道我可以遍历类的 PPropList 属性列表并按名称访问属性,但是我必须使用如下代码:

isValidProperty('responseCode');

并以字符串形式传递属性名称,而不是像第一个 isValidProperty() 那样直接传递属性多于。有办法做到这一点吗?

I have a Delphi 6 class object that contains an array of 30 Variants, each of which is exposed via a different indexed property. For example:

property responseCode: integer
        Index 7 read getIndexedProperty_integer write setIndexedProperty_integer;

I did this to make using the array of Variants easier (helps the IDE's auto-complete) and to provide type safety. It works fine but now I have a wrinkle. The array of Variants are initialized to NULL when the class that wraps it is constructed, so I can tell if a particular variant has ever been instantiated with a value. A consequence of this is if only some of the Variants are instantiated (given valid values), any attempt to access a property that currently represents a NULL Variant will cause a Variant conversion error when Delphi tries to convert the variant to the type declared by the indexed property.

I would much rather not declare an "isValid" property for each indexed property. I was wondering if there was a way to use the TypeInfo library to get the raw value of the underlying Variant without having to access the indexed property directly and thus triggering the conversion Exception. Then I could write code like (using the example property above):

isValidProperty(responseCode);

and that function would return TRUE if the Variant underlying the responseCode property is not NULL and FALSE if it is.

I know I can walk the PPropList property list for the class and access the properties by name, but then I would have to use code like:

isValidProperty('responseCode');

and pass the property name in string form instead of passing in the property directly like the first isValidProperty() above. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2025-01-05 18:59:41

因此,您希望“获取基础 Variant 的原始值,而不必直接访问索引属性,从而触发转换异常”。只要您可以访问底层变体本身,就可以。您很可能需要更改容器类本身。

来自有关变体类型的 Delphi XE2 帮助页面

标准函数VarType返回变体的类型代码。这
varTypeMask 常量是一个位掩码,用于从中提取代码
VarType 的返回值,例如,

VarType(V) and varTypeMask = varDouble

如果 V 包含 Double 或一个 True,则返回 True
双精度数组。 (掩码只是隐藏第一位,这表明
变体是否包含数组。)定义的 TVarData 记录类型
System 单元中可用于类型转换变体并获得对
他们的内部代表。

您应该能够结合使用此处提到的方法和记录来查找有关变体内部数据的任何信息,包括它是否为 NULL 变体,以及直接访问它。

(这个系统对我来说似乎有点狡猾的设计:它似乎不是一个非常类型安全的实现......请参阅上面的评论。我认为基于您期望的值的实际类型的设计可能会更安全。但是,这个会让你实现你的目标。)

So you want "to get the raw value of the underlying Variant without having to access the indexed property directly and thus triggering the conversion Exception". So long as you can access the underlying Variant itself, yes, you can. You will need to change the container class itself most likely.

From the Delphi XE2 help page on variant types:

The standard function VarType returns a variant's type code. The
varTypeMask constant is a bit mask used to extract the code from
VarType's return value, so that, for example,

VarType(V) and varTypeMask = varDouble

returns True if V contains a Double or an
array of Double. (The mask simply hides the first bit, which indicates
whether the variant holds an array.) The TVarData record type defined
in the System unit can be used to typecast variants and gain access to
their internal representation.

You should be able to use a combination of the methods and records mentioned here to find out anything you want about the internal data inside the variant, including if it's a NULL variant, as well as getting direct access to it.

(This system seems slightly dodgy design to me: it doesn't seem a very type safe implementation... see my comment above. I think a design based on the actual types of the values you are expecting might be safer. But, this will let you achieve your goal.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文