Title: Enumerated set to String
Question: A cast like String(EnumSet) doesn't work on sets, so here is a function to transform any Enumerated set in String type.
Answer:
uses System, TypInfo;
function EnumValuesToString(PEnumSet: PTypeInfo; ADelimiter: String = ','): String;
var
PEnumData: PTypeData;
I: Integer;
begin
Result := '';
if (PEnumSet^.Kind = tkSet) then
PEnumSet := TypInfo.GetTypeData(PEnumSet)^.CompType^; //If a Set, get the enum type the set is based on
if not (PEnumSet^.Kind = tkEnumeration) then // TypeInfo not compatible for EnumValuesToString
Exit;
PEnumData := TypInfo.GetTypeData(PEnumSet);
for I := 0 to PEnumData^.MaxValue do
if I = PEnumData^.MaxValue then
Result := Result + System.Copy(TypInfo.GetEnumName(PEnumSet, I), 1, $FF)
else
Result := Result + System.Copy(TypInfo.GetEnumName(PEnumSet, I), 1, $FF) + ADelimiter;
end;
How to use it
-------------
type
TAnyEnumerated = (aeFirst, aeSecond, aeThird, aeFourth);
TEnumeratedSets = set of TAnyEnumerated;
...
var
EnumString: String;
begin
EnumString := EnumValuesToString(TypeInfo(TAnyEnumerated));
// EnumString -- 'aeFirst,aeSecond,aeThird,aeFourth'
EnumString := EnumValuesToString(TypeInfo(TEnumeratedSets));
// EnumString -- 'aeFirst,aeSecond,aeThird,aeFourth'
end;
...