delphiclrdelphi-xejedi-code-library

Returning complex types (Classes, Arrays of Classes) using JCL and CLR


I first referenced this question to get started, but reached a roadblock when trying to return a Class or Array of a Class using a .NET Assembly in Delphi XE.

Consider the following:

//C#
[ComVisible(true)]
public class Person {
    public int Id;
    public string Name;
}

public class SomeClass 
{
    public SomeClass() {}        

    public Person[] GetPersons()
    {
        //some code
    }
}

//Delphi
type TPerson = class
  Id : Integer;
  Name : string;
end;

How do I make sense of the data that is returned from GetPersons() which I can assign to an array of TPerson in Delphi?


Solution

  • You can't, at least not that way. Delphi and .NET have different object models, and different string types, which aren't compatible with each other. If you want to pass objects between .NET and Delphi modules, the best way is probably to use COM.