I want to convert a string to an enum type using TValue, I googled, but I did not find how to do this.
type
TEnumTest = (etFirst, etSecond);
var
D: TEnumTest;
begin
D := StrToENumTest('etFirst');
end;
function StrToEnumTest(pStr:String):TEnumTest;
var
V: TValue;
begin
V := TValue.From<String>(pstr);
Result := V.AsType<TEnumTest>;
end;
This does not work. It must be something stupid that I do not see - but I did not find it. What did I do wrong?
I know how to use GetEnumValue.
EDIT: @Warren, it goes here, as it is easier to send the code:
TEnumUtils = class
class function GetAs<T>(pValor: String): T;
end;
class function TEnumUtils.GetAs<T>(pValor: String): T;
var
Tipo: PTypeInfo;
Temp: Integer;
PTemp: Pointer;
begin
Tipo := TypeInfo(T);
Temp := GetEnumValue(Tipo, pValor);
PTemp := @Temp;
Result := T(PTemp^);
end;
Using:
type
TEnumTest = (etFirst, etSecond);
var
D: TEnumTest;
begin
D := TEnumUtils.GetAs<TEnumTest>('etFirst');
end;
source
share