如何使用TypInfo单元中的GetSetProp和SetSetProp

时间:2010-01-24 12:50:05

标签: delphi

我有一组枚举值,我需要将其转换为文本然后再转换为集合。

我相信来自TypInfo单元的GetSetProp和SetSetProp会允许这样做,但我不知道如何让它工作。关于如何使用GetSetProp和SetSetProp来实现这个目标的任何想法?

type
  TSomething = (sOne, sTwo, sThree, sFour, s Five);
  TSomethings = set of TSomething;

var
  Something: TSomethings;
  s: string;
...
  Something := [sOne, sThree];

  s := GetSetProp(????);

  Something := [];
  // then use SetSetProp to set Something back to [sOne, sThree]
  Something := ????

2 个答案:

答案 0 :(得分:4)

great post on SetToString, StringToSet tondrej解决了您的问题。

答案 1 :(得分:2)

由于方法名称可能会导致:这仅适用于已发布的属性!

type
  TSomething = (sOne, sTwo, sThree, sFour, sFive);
  TSomethings = set of TSomething;
  TSomeClass = class
  private
    FSomeThing: TSomethings;
  public
  published
    property SomeThing: TSomethings read FSomeThing write FSomeThing;
  end;

...
var
  SomeClass: TSomeClass;
  s: string;
begin
  SomeClass := TSomeClass.Create;
  try
    SomeClass.Something := [sOne, sThree];
    s := GetSetProp(SomeClass, 'Something');
    ...
  finally
    SomeClass.Free;
  end;