Variant 内容类型的字符串表示形式?
首先,对我的英语表示歉意,我希望我在这里写的内容有意义。 现在我的问题。
如何使用 TypInfo.GetEnumName() 获取变体内容类型的字符串表示形式。 我尝试了以下操作,但没有运气,我得到了数字表示。
myString := GetEnumName( TypeInfo(TVarType), TVarData(myVar).VType );
谢谢。
First, apologies for my English, I hope it makes sense what I`ve written here. Now to my problem.
How can I get the string representation of the content type of a Variant using TypInfo.GetEnumName(). I have tried the following, without luck, I get a numeric representation.
myString := GetEnumName( TypeInfo(TVarType), TVarData(myVar).VType );
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用内置的 Delphi 函数即可获取 Variant 类型的字符串表示形式。
Just use the build-in Delphi function for getting the string representation of a Variant type.
引用Delphi 2007帮助:
这意味着您不能将它用于此目的,因为 TVarData.VType 不是枚举值,而是一个整数,它被设置为 System.pas 中的常量之一,这些常量取自Windows SDK wtypes.h 文件。 查看GetEnumName()的源代码,它确实立即返回一个包含整数值的字符串。
编辑:
您可以手动确定字符串表示形式。 首先,您需要意识到该整数中编码了几位信息,因此简单的 case 语句或数组查找将不起作用。 低 12 位是类型掩码,高位编码有关它是向量还是数组类型以及是否通过引用给出的信息。 重要的部分是:
所以你可以这样做:
Quoting from the Delphi 2007 help:
That means that you can't use it for that purpose, as TVarData.VType is not an enumerated value, but an integer which is set to one of the constants in System.pas that are taken from the Windows SDK wtypes.h file. Look at the source of GetEnumName(), it does immediately return a string containing the value of the integer.
Edit:
You can determine the string representation manually. First you need to be aware of that there are several bits of information encoded in that integer, so a simple case statement or array lookup will not work. The lower 12 bits are the type mask, and the upper bits encode information about whether it is a vector or array type and whether it is given by reference or not. The important parts are:
So you could do something like:
由于它不是枚举,因此您必须手动执行此操作。 编写如下内容:
或者,由于 System.pas 中的值是按顺序列出的,因此您可以尝试声明一个 const 字符串数组,并让您的 VariantTypeName 函数返回该数组的适当成员。
Since it's not an enum, you'll have to do it manually. Write something like this:
Or, since the values in System.pas are listed in order, you could try declaring a const array of strings and have your VariantTypeName function return the appropriate member of the array.
对于不支持 VarTypeAsText 的 Delphi 版本,这里有一个想法:您可以自己定义一个遵循 VType 值的枚举类型:(
也填充未使用的枚举槽 - 请参阅 为什么我会收到枚举类型的“type has no typeinfo”错误这背后的推理)。
接下来,使用这些函数将 Variants 类型读取为您自己的枚举类型:
然后您可以将其转换为如下字符串:
Here's a thought for Delphi versions that don't support VarTypeAsText: You could define a enumerate type yourself that follows the VType values:
(Fill the unused enum slots too - see Why do I get "type has no typeinfo" error with an enum type for the reasoning behind this).
Next, use these functions to read the Variants' type as your own enumerate type :
And then you can convert it to a string like this :