加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何通过Soap公开Delphi集类型

发布时间:2020-12-15 04:12:50 所属栏目:大数据 来源:网络整理
导读:我目前正在为一些Delphi函数创建soap包装器,以便我们可以轻松地从 PHP,C#和Delphi中使用它们. 我想知道暴露集合的最佳方式是什么. type TCountry = (countryUnknown,countryNL,countryD,countryB,countryS,countryFIN,countryF,countryE,countryP,countryPl,
我目前正在为一些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;

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

type 
  TCountryArray = array of TCountry;

function TVehicleInfo.GetValidCountrySet(const LicensePlate:string; const PossibleCountriesSet:TCountryArray):TCountryArray;

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

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

解决方法

您可以使用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;

然后用作:

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;

您需要根据TCountry枚举的大小选择合适的铸件.如果它有8个成员,它将是一个Byte,任何更大的,它将是一个Integer.无论如何,当你弄错了时,德尔福会对Byte(国家)或整数(国家)的演员投诉.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读