[英]How can I assign an interface variable to a variable of type Rtti.TValue
At the moment I give delphi2010 a trial and found the TValue type of the Rtti Unit. 目前我给delphi2010一个试验,发现了TVtue类型的Rtti单元。 TValue have very interessting features, but I can't find a way to assign an interface.
TValue具有非常有趣的功能,但我找不到分配界面的方法。
I try the following 我尝试以下方法
program Project1;
uses
Classes, SysUtils, Rtti;
var
list : IInterfaceList;
value : TValue;
begin
// all these assignments works
value := 1;
value := 'Hello';
value := TObject.Create;
// but nothing of these assignments works
list := TInterfaceList.Create;
value := list; // [DCC Fehler] Project1.dpr(15): E2010 incompatible types: 'TValue' and 'IInterfaceList'
value.From[list]; // [DCC Fehler] Project1.dpr(16): E2531 Method 'From' requires explicit typarguments
value.From<IInterfaceList>[list]; // [DCC Fehler] Project1.dpr(17): E2035 Not enough parameters
end.
I can't find any further information. 我找不到任何进一步的信息。 Not in the delphi help system and not on the internet.
不是在delphi帮助系统而不是在互联网上。 What do I do wrong?
我做错了什么?
This is a working version of the program: 这是该程序的工作版本:
program Project1;
uses
Classes, SysUtils, Rtti;
var
list : IInterfaceList;
value : TValue;
begin
// all these assignments works
value := 1;
value := 'Hello';
value := TObject.Create;
// but nothing of these assignments works
list := TInterfaceList.Create;
value := TValue.From(list);
end.
Your last try is the closest. 你的最后一次尝试是最接近的。 TValue.From is a class function that creates a TValue from a parameter.
TValue.From是一个类函数,它从参数创建一个TValue。 You probably put the square brackets in there because that's how CodeInsight showed it, right?
你可能把方括号放在那里,因为这是CodeInsight展示的方式,对吧? That's actually a glitch in CodeInsight;
这实际上是CodeInsight的一个小故障; it does that for generics-based functions, where you should be using parenthesis instead.
它适用于基于泛型的函数,您应该使用括号。 The proper syntax looks like this:
正确的语法如下所示:
Value := TValue.From<IInterfaceList>(list);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.