简体   繁体   English

如何将接口变量分配给Rtti.TValue类型的变量

[英]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.

相关问题 如何测试未知的Delphi RTTI TValue是否反映了任何类型的通用TList <>(或至少TEnumerable <>)的对象? - How can I test if an unknown Delphi RTTI TValue reflects an object that is ANY type of generic TList<> (or at least TEnumerable<>)? 如何为枚举的RTTI字段创建通用TValue? - How do I create a generic TValue for enumerated RTTI field? Delphi Assembler / RTTI-gurus:我可以获取函数中隐含的Result变量的内存地址和类型信息吗? - Delphi Assembler/RTTI-gurus: Can I obtain the memory address and type info of the implied Result variable in a function? 如何为RTTI分配OleVariant? //在考虑特定的TTypeKind或TRTTIType的情况下将OleVariant或Variant转换为TValue? - How to assign an OleVariant with RTTI? // Convert an OleVariant or a Variant to a TValue with specific TTypeKind or TRTTIType in mind? 如何使用RTTI获取数组的元素类型 - How I can get the element type of an array using RTTI 使用 RTTI 从已知类型在 Delphi 中设置 TValue 记录 - Setting TValue record in Delphi from known type with RTTI RTTI:我可以按姓名获取类型吗? - RTTI: Can I Get a Type by Name? 在Delphi XE3中,如何使用TypeInfo或RTTI将TVirtualInterface对象转换为其接口? - In Delphi XE3, how can I cast a TVirtualInterface object to its interface, using TypeInfo or RTTI? 具有接口类型约束的通用类型的RTTI - RTTI for generic type with interface type constraint 使用RTTI获取变量名称 - Get Variable Name Using RTTI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM