开发者

How can I deserialize properties which don't have an in built converter?

This is a spin off from my last question How does delphi convert ModalResult properties?

Since Delphi doesn't convert ModalResult properties, what's the best way for me to convert ModalResult properties to integers?

I don't really want:

If SpecialCase then
else if AnotherSpecialCase then
else BehaveNormally

So how do I convert values such as 'mrOk' into 1?

Note: I'm using

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

to set the property values.

Delphi 2开发者_StackOverflow社区007


There is no converter for ModalResults, Delphi stores the Integer representation in the DFM. As a solution I've registered a new converter

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜