3
我希望能够使用TValue将数据存储在TList <>中。像:Delphi 2010 RTTI:使用TValue存储数据
type
TXmlBuilder = class
type
TXmlAttribute = class
Name: String;
Value: TValue; // TValue comes from Rtti
end;
TXmlNode = class
Name: String;
Parent: TXmlNode;
Value: TXmlNode;
Attributes: TList<TXmlAttribute>;
Nodes: TList<TXmlNode>;
function AsString(Indent: Integer): String;
end;
...
public
...
function N(const Name: String): TXmlBuilder;
function V(const Value: String): TXmlBuilder;
function A(const Name: String; Value: TValue): TXmlBuilder; overload;
function A<T>(const Name: String; Value: T): TXmlBuilder; overload;
...
end;
implementation
function TXmlBuilder.A(const Name: String; Value: TValue): TXmlBuilder;
var
A: TXmlAttribute;
begin
A := TXmlAttribute.Create;
A.Name := Name;
A.Value := Value;
FCurrent.Attributes.Add(A);
Result := Self;
end;
function TXmlBuilder.A<T>(const Name: String; Value: T): TXmlBuilder;
var
V: TValue;
begin
V := TValue.From<T>(Value);
A(Name, V);
end;
和位后,在主程序中,我用我的“流利” XML构建这样的:
b := TXmlBuilder.Create('root');
b.A('attribute', 1).A('other_attribute', 2).A<TDateTime>('third_attribute', Now);
在第二个电话时,程序会引发访问冲突异常。
看起来第一个TValue已被“释放”。是否真的有可能使用TValue在运行时存储“Variant”数据?
我知道变种存在于Delphi中。我的XML构建器将用于(使用RTTI将本地delphi对象序列化为XML,所以我将在任何地方使用TValue。
问候,
- 皮埃尔雅格尔
没有编译器警告你了吗? – 2010-01-26 14:31:52
不,但使用泛型隐藏了大量有用的东西。例如,调试器不再为通用代码启用。 – ZeDalaye 2010-01-26 17:14:10