转换类型的值变成变体,有可能吗?
这是一个显示我想要实现的目标的片段:
type
TMyObject<T> = class (TObject)
function GetVarType(Value: T): TVarType;
end;
function TMyObject<T>.GetVarType(Value: T): TVarType;
var
TmpValue: Variant;
begin
TmpValue := Variant(Value); //Invalid typecast
Result := VarType(TmpValue);
end;
我知道上面的类型转换方法很幼稚,但我希望你明白。我想用一些转换机制来代替它。
TMyObject 将始终是简单类型,如 Integer、String、Single、Double。
这种转换的目的是函数 VarType 为每个简单类型提供整数常量,我可以将其存储在其他地方。
我想知道这样的转换是否可行?
感谢您抽出时间。
here is a snippet showing what I am trying to achieve:
type
TMyObject<T> = class (TObject)
function GetVarType(Value: T): TVarType;
end;
function TMyObject<T>.GetVarType(Value: T): TVarType;
var
TmpValue: Variant;
begin
TmpValue := Variant(Value); //Invalid typecast
Result := VarType(TmpValue);
end;
I know that above apporach with typecast is naive but I hope you get the idea. I would like to replace it with some conversion mechanism.
TMyObject will be always of simple type like Integer, String, Single, Double.
The purpose of such conversion is that function VarType gives me integer constant for each simple type which I can store somewhere else.
I would like to know if such conversion is possible?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它可以在具有增强 RTTI(2010 及更新版本)的 Delphis 中轻松解决。太糟糕了,您仅限于 2009 年:(
这仅适用于简单类型,但这是问题中指定的约束。
It's trivially solvable in Delphis with enhanced RTTI (2010 and newer). Too bad you're limited to 2009 :(
This works only for simple types but that was a constraint specified in the question.
您可以使用 RTTI 来获取此信息,只需检查
TTypeInfo.Kind
属性的值:检查此示例代码,
这将返回
You can use the RTTI to get this info, just check the value of the
TTypeInfo.Kind
property:Check this sample code
this will return
我不明白如何使用泛型来做到这一点。编译器需要知道类型
T
的实例可以分配给任何可能的T
的Variant
。您无法告诉编译器这是可能的。如果这是 C++ 中的模板,那么这将是微不足道的。
I can't see how it could be possible to do this with generics. The compiler needs to know that an instance of type
T
can be assigned to aVariant
for any possibleT
. There's no way for you to tell that compiler that is possible.If this were templates as in C++ then it would be trivial.
感谢大家的回答:
正如 @RRUZ 所表明的那样,这是可能的(我的意思不是严格分配,而是提取数据类型)。我在等待任何答案时自己工作,并找到了更通用的解决方案。
所以我把它放在这里:
再次感谢!
Thanks guys for your answers:
As @RRUZ have shown it is possible (I mean not strict assigment but extracting the type of data). I was working on my own while waiting for any answer and have found a more generic solution.
So I am positing it here:
Once again thanks!