如何将 PSafeArray 类型变量分配给对象数组类型变量?

时间:2021-02-04 18:50:20

标签: arrays object

我有一个像下面这样的对象。

unit MyResponseModel;

interface


type
  TMyResponse = class
  private
    FStatus: integer;
    FSorguNo: string;
    FStatusMessage: string;
    { private declarations }
  protected
    { protected declarations }
  public
    { public declarations }
    property Status: Integer read FStatus write
      FStatus;
    property SorguNo: string read FSorguNo write
      FSorguNo;
    property  StatusMessage: string read FStatusMessage write
      FStatusMessage;

    constructor Create();
    destructor Destroy(); override;
  published
    { published declarations }
  end;

implementation

{ TRequest }

constructor TMyResponse.Create();
begin
     inherited Create;
end;

destructor TMyResponse.Destroy;
begin
  inherited Destroy;
end;

end.

举个例子,我有一个包含数组对象的 JSON 文件。

[
  {
    "Status": 0,
    "StatusMessage": "Status Message 0",
    "SorguNo": "Sorgu No 0"
  },
  {
    "Status": 1,
    "StatusMessage": "Status Message 1",
    "SorguNo": "Sorgu No 1"
  },
  {
    "Status": 2,
    "StatusMessage": "Status Message 2",
    "SorguNo": "Sorgu No 2"
  }
]

我在 C# 中有一个 dll,它声明了我使用 NewtonSoft 库编写的 Json 文件。 下面是我在这个dll中使用的方法。

 public MyResponse[] DeSerializeFromJsonFileToObjectArray(string fileName)
        { 
            string jsonString = File.ReadAllText(fileName);
            try
            {
                var model = JsonConvert.DeserializeObject<MyResponse[]>(jsonString);
                return model;
            }
            catch (Exception)
            {
                throw;
            } 
        } 
    
    

我在 delphi 中将此 dll 作为 TypeLibrary 导入。

自然而然地,typeLibrary 中的这个方法变成了下面的形式。

function TIndemSoftJsonHelper.DeSerializeFromJsonFileToObjectArray(const fileName: WideString): PSafeArray;
begin
  Result := DefaultInterface.DeSerializeFromJsonFileToObjectArray(fileName);
end;

dll 在我的 MyResponse 类中。该类位于名为 _MyResponse 的 tlb 文件中。

自然,它不会作为完整类型返回。我刚刚进入这个话题。导入时,我希望从方法返回类似 _MyResponse 的数组。 : 哈哈: 但现在我明白了,它以 PSafeArray 的形式返回是完全正常的。

我在 tlb 文件中调用我的方法如下。

var
  myArray: PSafeArray;
 responsearray: array of _MyResponse; 
begin
  myArray := MyDll.DeSerializeFromJsonFileToObjectArray('C:\Users\Asus\Desktop\Temp\deneme.json');
 //The json file I gave as an example above 


 

我在这里卡住了。 该函数给了我一个 PsafeArray 类型的返回值。我把它带到名为 myarray 的变量中。 但是现在我必须阅读这些并将其强制转换为 _MyResponse 或 MyResponse 类型的变量,其中包含已知属性。 但是如何? 我搜索了很多,但找不到。 如果有知识的朋友帮助,将不胜感激。 提前致谢.....

0 个答案:

没有答案