uses TypInfo;type TSetDemo = set of(S1, S2, S3, S4);procedure StrToSet(TypeInfo: PTypeInfo;
const Value: string;
out Result);
function NextWord(var P: PChar): string;
var I: Integer;
begin
while P^ in [',', ' ', '[', ']']do
Inc(P);
I := 0;
while P in ['A'..'Z', 'a'..'z', '0'..'9', '_']do
Inc(I);
SetString(Result, P, I);
Inc(P, I);
end;
type TMaxSet = set of Byte;var ElemTypeInfo: PTypeInfo;
ElemTypeData: PTypeData;
P: PChar;
EnumName: string;
I, ElemValue: Integer;
begin
ElemTypeInfo := GetTypeData(TypeInfo)^.CompType^;
ElemTypeData := GetTypeData(ElemTypeInfo);
for I := ElemTypeData^.MinValue to ElemTypeData^.MaxValuedo
Exclude(TMaxSet(Result), I);
P := PChar(Value);
repeat EnumName := NextWord(P);
if EnumName = '' then
Break;
ElemValue := GetEnumValue(ElemTypeInfo, EnumName);
if ElemValue < 0 then
raise EConvertError.CreateFmt('值"%s"不正常吧!', [EnumName]);
Include(TMaxSet(Result), ElemValue);
until False;
end;
function SetToStr(TypeInfo: PTypeInfo;
const Value;
Brackets: Boolean = True): string;type ByteSet = set of 0..7;var ByteValue: ^ByteSet;
I, M: Integer;
ElemTypeInfo: PTypeInfo;
ElemTypeData: PTypeData;
begin
Result := '';
ElemTypeInfo := GetTypeData(TypeInfo)^.CompType^;
ElemTypeData := GetTypeData(ElemTypeInfo);
ByteValue := @Value;
for I := ElemTypeData^.MinValue to ElemTypeData^.MaxValuedo
begin
M := I mod 8;
if M in ByteValue^ then
begin
if Result <> '' then
Result := Result + ',';
Result := Result + GetEnumName(ElemTypeInfo, I);
end;
if M = 7 then
Inc(ByteValue) end;
if Brackets then
Result := '[' + Result + ']';
end;
procedure TForm1.Button1Click(Sender: TObject);var S : TSetDemo;
begin
StrToSet(TypeInfo(TSetDemo), '[S1,S3]', s);
Edit1.Text := SetToStr(TypeInfo(TSetDemo),S, True);
end;