How to assign an interface variable to a variable of type Rtti.TValue
I am giving delphi2010 a trial at the moment and found the TValue type of the Rtti module. TValue has some very cool features, but I can't seem to find a way to assign the interface.
I will 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 cannot find any further information. Not on the delphi help system, not on the web. What am I doing wrong?
+2
source to share
2 answers
Your last try is your closest. TValue.From is a class function that creates TValue from a parameter. You probably put square brackets in there because, as CodeInsight showed, right? This is actually a glitch in CodeInsight; it does this for generic functions, where you should use parentheses instead. The correct syntax is as follows:
Value := TValue.From<IInterfaceList>(list);
+6
source to share
This is the 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.
+7
source to share