How to use GetSetProp and SetSetProp from TypInfo unit

喜夏-厌秋 提交于 2019-12-30 13:35:15

问题


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

回答1:


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




回答2:


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;


来源:https://stackoverflow.com/questions/2127079/how-to-use-getsetprop-and-setsetprop-from-typinfo-unit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!