如何通过Soap公开Delphi集类型

Wou*_*ick 6 delphi soap set

我目前正在为一些Delphi函数创建soap包装器,以便我们可以轻松地从PHP,C#和Delphi中使用它们.

我想知道暴露集合的最佳方式是什么.

type
  TCountry     = (countryUnknown,countryNL,countryD,countryB,countryS,countryFIN,countryF,countryE,countryP,countryPl,countryL);
  TCountrySet  = set of TCountry;

function GetValidCountrySet(const LicensePlate:string; const PossibleCountriesSet:TCountrySet):TCountrySet;
Run Code Online (Sandbox Code Playgroud)

我正在为肥皂服务器包装它:

type 
  TCountryArray = array of TCountry;

function TVehicleInfo.GetValidCountrySet(const LicensePlate:string; const PossibleCountriesSet:TCountryArray):TCountryArray;
Run Code Online (Sandbox Code Playgroud)

它可以工作,但我需要编写很多无用且丑陋的代码来转换集 - >数组和数组 - >集.

是否有更简单,更优雅或更通用的方法来做到这一点?

Mar*_*ema 3

您可以使用 TypInfo 并使用一些巧妙的转换。

uses TypInfo;

type
  TCountry = (cnyNone, cnyNL, cnyD, cnyGB, cnyF, cnyI);
  TCountrySet = set of TCountry;

  TCountryArray = array of TCountry;
  TEnumIntegerArray = array of Integer;
  TEnumByteArray = array of Byte;

function GetEnumNamesInSet(const aTypeInfo: PTypeInfo; const aValue: Integer; const aSeparator: string = ','): string;
var
  IntSet: TIntegerSet;
  i: Integer;
begin
  Result := '';
  Integer( IntSet ) := aValue;
  for i := 0 to SizeOf(Integer) * 8 - 1 do begin
    if i in IntSet then begin
      if Result <> '' then begin
        Result := Result + ',';
      end;
      Result := Result + GetEnumName(aTypeInfo, i);
    end;
  end;
end;

function SetToIntegerArray(const aTypeInfo: PTypeInfo; const aValue: Integer): TEnumIntegerArray;
var
  IntSet: TIntegerSet;
  i: Integer;
begin
  SetLength(Result, 0);
  Integer( IntSet ) := aValue;
  for i := 0 to SizeOf(Integer) * 8 - 1 do begin
    if i in IntSet then begin
      SetLength(Result, Length(Result) + 1);
      Result[Length(Result) - 1] := i;
    end;
  end;
end;

function SetToByteArray(const aTypeInfo: PTypeInfo; const aValue: Byte): TEnumByteArray;
var
  IntSet: TIntegerSet;
  i: Integer;
begin
  SetLength(Result, 0);
  Integer( IntSet ) := aValue;
  for i := 0 to SizeOf(Byte) * 8 - 1 do begin
    if i in IntSet then begin
      SetLength(Result, Length(Result) + 1);
      Result[Length(Result) - 1] := i;
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

然后用作:

procedure TEnumForm.FillMemo;
var
  Countries: TCountrySet;
//  EIA: TEnumIntegerArray;
  EBA: TEnumByteArray;
  CA: TCountryArray;
  i: Integer;
  cny: TCountry;
begin
  Countries := [cnyNL, cnyD];
  CountriesMemo.Text := GetEnumNamesInSet(TypeInfo(TCountry), Byte(Countries));
//  if SizeOf(TCountry) > SizeOf(Byte) then begin
//    EIA := SetToIntegerArray(TypeInfo(TCountry), Integer(Countries));
//  end else begin
    EBA := SetToByteArray(TypeInfo(TCountry), Byte(Countries));
//  end;
  CountriesMemo.Lines.Add('====');
  CountriesMemo.Lines.Add('Values in Array: ');
//  if SizeOf(TCountry) > SizeOf(Byte) then begin
//    CA := TCountryArray(EIA);
//  end else begin
    CA := TCountryArray(EBA);
//  end;
  for i := 0 to Length(CA) - 1 do begin
    CountriesMemo.Lines.Add(IntToStr(Ord(CA[i])));
  end;
  CountriesMemo.Lines.Add('====');
  CountriesMemo.Lines.Add('Names in Array: ');
//  if SizeOf(TCountry) > SizeOf(Byte) then begin
//    CA := TCountryArray(EIA);
//  end else begin
    CA := TCountryArray(EBA);
//  end;
  for i := 0 to Length(CA) - 1 do begin
    cny := CA[i];
    CountriesMemo.Lines.Add(GetEnumName(TypeInfo(TCountry), Ord(cny)));
  end;
end;
Run Code Online (Sandbox Code Playgroud)

您需要根据 TCountry 枚举的大小选择适当的转换。如果它有 8 个成员,它将是一个字节,如果更大,它将是一个整数。不管怎样,当你出错时,Delphi 会抱怨 Byte(Countries) 或 Integer(Countries) 的转换。

请注意:这些函数现在采用 TCountry 的 TypeInfo - TCountrySet 的元素。可以将它们更改为采用 TypeInfo(TCountrySet)。然而,这意味着让函数计算出集合中的元素,而我只是还没有时间或意愿这样做。