将TObjectlist转换成TObjectList< T>的过程。在TObjectDataset中使用 [英] Process to convert TObjectlist to TObjectList<T> to use in TObjectDataset

查看:88
本文介绍了将TObjectlist转换成TObjectList< T>的过程。在TObjectDataset中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用依赖于TObjectList的TObjectDataset(System.Generics.Collections / Spring.Collections),但只有一个TObjectList(System.Contnrs)。除了遍历对象并构建新的TObjectList<>来使此工作正常进行之外,还有其他方法吗?最终,我想将TObjectList耦合到Objectdataset以便绑定到UI。

I would like to use TObjectDataset which relies on TObjectList<> (System.Generics.Collections / Spring.Collections) but only have a TObjectList (System.Contnrs). Is there any way besides for iterating through objects and building a new TObjectList<> to get this working? Ultimately I would like to couple the TObjectList to an Objectdataset in order to bind to an UI.

推荐答案

您的问题有点错误。 Spring4d TObjectDataSet采用 IObjectList 接口,这是 IList< T> 的特化,其中 T TObject

Your question is slightly wrong. The Spring4d TObjectDataSet takes an IObjectList interface which is a specialization of IList<T> where T is TObject.

此合同与匹配Contnrs.TObjectList 。因此,简单地为实现 IObjectList TObjectList 创建包装器类。我简单地用引号引起来,因为此接口有很多方法。您可以将 TListBase< T> 用作适配器的基类,该适配器已经实现了所有方法。然后,您只需要覆盖一些(看看那些是 TList< T> )。

This contract is matched by the Contnrs.TObjectList. So "simply" create a wrapper class for your TObjectList that implements IObjectList. I put simply in quotes because this interface has quite a lot of methods. You can use TListBase<T> as base class for your adapter which already has all methods implemented. Then you only need to override a few (take a look at TList<T> which ones those are).

要知道的一个重要细节是 TObjectDataSet 需要知道列表中对象的确切类。这是通过 IObjectList ElementType 属性完成的。如果返回 TObject ,虽然这不是很有帮助。因此,您需要重写该方法。

One important detail to know is that the TObjectDataSet needs to know the exact class of the objects in your list. This is done via the ElementType property of the IObjectList. If that returns TObject though this is not very helpful. So you need to override that method.

编辑:以下是此类适配器类的完整代码:

Here is the full code of such an adapter class:

unit Spring.Collections.ObjectListAdapter;

interface

uses
  Contnrs,
  TypInfo,
  Spring.Collections,
  Spring.Collections.Base;

type
  TObjectListAdapter = class(TListBase<TObject>, IObjectList)
  private
    fList: TObjectList;
    fClassType: TClass;
  protected
    function GetCapacity: Integer; override;
    function GetCount: Integer; override;
    function GetElementType: PTypeInfo; override;
    function GetItem(index: Integer): TObject; override;
    procedure SetCapacity(value: Integer); override;
    procedure SetItem(index: Integer; const value: TObject); override;
  public
    constructor Create(const list: TObjectList; classType: TClass);

    procedure Delete(index: Integer); override;
    function Extract(const item: TObject): TObject; override;
    procedure Insert(index: Integer; const item: TObject); override;

    procedure Exchange(index1, index2: Integer); override;
    procedure Move(currentIndex, newIndex: Integer); override;
  end;

implementation

uses
  Classes,
  Types;

{ TObjectListAdapter }

constructor TObjectListAdapter.Create(const list: TObjectList; classType: TClass);
begin
  inherited Create;
  fList := list;
  fClassType := classType;
end;

procedure TObjectListAdapter.Delete(index: Integer);
begin
  fList.Delete(index);
end;

procedure TObjectListAdapter.Exchange(index1, index2: Integer);
begin
  fList.Exchange(index1, index2);
end;

function TObjectListAdapter.Extract(const item: TObject): TObject;
begin
  Result := fList.Extract(item);
end;

function TObjectListAdapter.GetCapacity: Integer;
begin
  Result := fList.Capacity;
end;

function TObjectListAdapter.GetCount: Integer;
begin
  Result := fList.Count;
end;

function TObjectListAdapter.GetElementType: PTypeInfo;
begin
  Result := fClassType.ClassInfo;
end;

function TObjectListAdapter.GetItem(index: Integer): TObject;
begin
  Result := fList[index];
end;

procedure TObjectListAdapter.Insert(index: Integer; const item: TObject);
begin
  fList.Insert(index, item);
end;

procedure TObjectListAdapter.Move(currentIndex, newIndex: Integer);
begin
  fList.Move(currentIndex, newIndex);
end;

procedure TObjectListAdapter.SetCapacity(value: Integer);
begin
  fList.Capacity := value;
end;

procedure TObjectListAdapter.SetItem(index: Integer; const value: TObject);
begin
  fList[index] := value;
end;

end.

这篇关于将TObjectlist转换成TObjectList&lt; T&gt;的过程。在TObjectDataset中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆