PDA

Ver la Versión Completa : SSLOptions. Parámetros por string.


Carmelo Cash
17-10-2019, 00:07:49
Hola Foro:

Quería saber si alguien sabe como puedo pasar el parámetro a SSLOpions desde un string.

por ejemplo:


Method:=q_Correo.fieldByname('Method').asstring;
SSLOptions.Method= fStrToAlgo(Method ); // << Acá es donde hay que arreglar esto



Actualmente estoy haciendo como muestro más abajo, pero queda muy largo sobre todo porque son otros parámetros para UseTLS, Mode, etc.


Method:=q_Correo.fieldByname('Method').asstring;

if Method= 'sslvTLSv1' then begin
SSLOptions.Method := sslvTLSv1 ;
end else
if Method = 'sslvSSLv2' then begin
SSLOptions.Method := sslvSSLv2 ;
end else
if Method = 'sslvSSLv23' then begin
SSLOptions.Method := sslvSSLv23 ;
end else
if Method = 'sslvSSLv3' then begin
SSLOptions.Method := sslvSSLv3 ;
end ;


Desde y agracias por su atención.

Neftali [Germán.Estévez]
17-10-2019, 10:04:47
Necesitas una clase que te convierta enumerados en strings y viceversa. Utilizando RTTI puedes hacerlo sin problemas.
Yo en mi caso utilizo esta:

// classHelper para conversiones
TEnumConverter = class
public
// Enumerado a entero
class function EnumToInt<T>(const EnumValue: T): Integer;
// enumerado a string
class function EnumToString<T>(EnumValue: T): string;
// class function StringToEnum<T>(AValor:string):T;
class procedure StringToEnum<T>(strValue:String; var enValue:T);
end;


Y la Implementacion:


// obtener string como int (enum de un tipo)
class function TEnumConverter.EnumToInt<T>(const EnumValue: T): Integer;
begin
Result := 0;
Move(EnumValue, Result, SizeOf(EnumValue));
end;

// Obtener enum como string
class function TEnumConverter.EnumToString<T>(EnumValue: T): string;
begin
Result := GetEnumName(TypeInfo(T), EnumToInt(EnumValue));
end;

// class function StringToEnum<T>(AValor:string):T;
class procedure TEnumConverter.StringToEnum<T>(strValue: String; var enValue:T);
var
Tipo: PTypeInfo;
Temp:Integer;
PTemp: pointer;
begin
Tipo := TypeInfo(T);
Temp := GetEnumValue(Tipo, strValue);
PTemp := @Temp;
enValue := T(PTemp^);
end;


Añade las units: System.Rtti, System.TypInfo

Y la forma de utilizarlo sería algo así:


procedure TForm1.Button1Click(Sender: TObject);
var
alineacion:TAlign;
i:Integer;
str:string;
begin
// enum a Cadena
alineacion := TAlign.alBottom;
ShowMessage('Alineacion como cadena: ' + TEnumConverter.EnumToString(TAlign(alineacion)));
// enum a Integer
i := TEnumConverter.EnumToInt(TAlign(alineacion));
ShowMessage('Alineacion como entero: ' + IntToStr(i));
// Cadena a enum
str := 'alRight';
TEnumConverter.StringToEnum(str, alineacion);
Button1.Align := alineacion;
end;

bucanero
17-10-2019, 10:36:11
hola

Aqui propongo otras alternativas valida para las versiones modernas de delphi, y es utilizar directamente las funciones de la RTTI

- Forma 1:

uses System.Rtti;

/// ...

function MethodAsString(const Method: TIdSSLVersion): String;
begin
Result := TRttiEnumerationType.GetName(Method);
end;

function StrToMethod(const MethodName: string):TIdSSLVersion;
begin
result := TRttiEnumerationType.GetValue<TIdSSLVersion>(MethodName);
end;


-Forma 2:
uses System.TypInfo;

function MethodAsString(const Method: TIdSSLVersion): String;
begin
result := GetENumName(TypeInfo(TIdSSLVersion), Ord(Method));
end;

function StrToMethod(const MethodName: string):TIdSSLVersion;
var
value:integer;
begin
value := GetEnumValue(TypeInfo(TIdSSLVersion), MethodName);
result := TIdSSLVersion(value);
end;


Y forma de utilizarlo:


var
Method: string;
begin
Method := q_Correo.fieldByname('Method').asstring;
with IdSSLIOHandlerSocketOpenSSL1 do begin
SSLOptions.Method := StrToMethod(Method);
// ...
MessageDlg(format('seleccionado %s ', [MethodAsString(SSLOptions.Method)]), mtInformation, [mbOK], 0);
end;