开发者

How to use GetSetProp and SetSetProp from TypInfo unit

I have a set of enumeration values that I need to convert to text and then back to a set.

I believe that GetSetProp and SetSetProp from TypInfo unit would allow to do this but I have no idea on to get it to work. Any idea on how I can use GetSetProp and SetSetProp to accomplish this?

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 := ????


This great post on SetToString, StringToSet by tondrej solves your problem.


As the method name might lead: this works only for published properties!

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜