如何反序列化没有内置转换器的属性?

时间:2010-08-03 09:50:44

标签: delphi serialization properties

这是我上一期的问题 How does delphi convert ModalResult properties?

由于Delphi不转换ModalResult属性,我将ModalResult属性转换为整数的最佳方法是什么?

我真的不想要:

If SpecialCase then
else if AnotherSpecialCase then
else BehaveNormally

那么如何将'mrOk'等值转换为1

注意:我正在使用

PropInfo := GetPropInfo(Instance, PropertyName);
SetPropValue(Instance, PropInfo, PropertyValue);

设置属性值。

Delphi 2007

1 个答案:

答案 0 :(得分:3)

ModalResults没有转换器,Delphi将整数表示存储在DFM中。作为解决方案,我注册了一个新的转换器

const
  ModalResults: array[0..10] of TIdentMapEntry = (
    (Value: mrNone; Name: 'mrNone'),           
    (Value: mrOk; Name: 'mrOk'),               
    (Value: mrCancel; Name: 'mrCancel'),       
    (Value: mrAbort; Name: 'mrAbort'),         
    (Value: mrRetry; Name: 'mrRetry'),         
    (Value: mrIgnore; Name: 'mrIgnore'),       
    (Value: mrYes; Name: 'mrYes'),             
    (Value: mrNo; Name: 'mrNo'),               
    (Value: mrAll; Name: 'mrAll'),             
    (Value: mrNoToAll; Name: 'mrNoToAll'),     
    (Value: mrYesToAll; Name: 'mrYesToAll'));



function ModalResultToIdent(ModalResult: Longint; var Ident: string): Boolean;
begin
    Result := IntToIdent(ModalResult, Ident, ModalResults);
end;

function IdentToModalResult(const Ident: string; var ModalResult: Longint): Boolean;
begin
    Result := IdentToInt(Ident, ModalResult, ModalResults);
end;
initialization
    RegisterIntegerConsts(TypeInfo(TModalResult), IdentToModalResult, ModalResultToIdent);