如何在泛型中实现 TCollection 和 TCollectionItem 功能

Posted

技术标签:

【中文标题】如何在泛型中实现 TCollection 和 TCollectionItem 功能【英文标题】:How to implement TCollection and TCollectionItem functionality in generics 【发布时间】:2014-10-19 12:20:37 【问题描述】:

我正在尝试在泛型中实现 TCollectionTCollectionItem 功能。为了解决这个问题,我们需要前向声明TGenericCollectionItemTGenericCollection。实际 (XE6) Delphi 编译器不支持使用泛型类型的前向声明。 There is a hack 如何制作。但是我们在将所有者分配给TCollectionItem 时仍然存在问题。

  TBaseElement = class // hack for forward declaration of a generic type 
  end;

  TGenericCollection<T: TBaseElement> = class(TObjectList<TBaseElement>)
  protected
    procedure Notify(const Value: TBaseElement; Action: TCollectionNotification); override;
  end;

  TGenericCollectionItem = class(TBaseElement)
  public
    Owner: TGenericCollection<TBaseElement>;
  end;

  procedure TGenericCollection<T>.Notify(
    const Value: TBaseElement; Action: TCollectionNotification);
  begin
    inherited;

    if (Action = cnAdded) then
    begin
      if (Value is TGenericCollectionItem) then
        (Value as TGenericCollectionItem).Owner := Self;  //here is error
    end;
  end;

E2010 不兼容的类型:'TGenericCollection&lt;TBaseElement&gt;''TGenericCollection&lt;TGenericCollection&lt;T&gt;.T&gt;'

如何解决这个冲突?

【问题讨论】:

泛型有什么用?您有一个非通用集合项的集合。泛型不需要这样做。 【参考方案1】:

你要找的是这段代码:

unit Generics.Legacy;


$WARN SYMBOL_DEPRECATED OFF // we know that Added and Deleting are deprecated
$HINTS OFF // we know we have intentionally hidden some overridden methods


interface


uses
  Classes;


type
  TCollection<T: TCollectionItem> = class(TCollection)
  private
    procedure Added(var Item: TCollectionItem); overload; override;
    procedure Deleting(Item: TCollectionItem); overload; override;
    procedure Notify(Item: TCollectionItem; Action: TCollectionNotification); overload; override;
    procedure SetItemName(Item: TCollectionItem); overload; override;
    procedure Update(Item: TCollectionItem); overload; override;
  protected
    procedure Added(var Item: T); reintroduce; overload; virtual; deprecated;
    procedure Deleting(Item: T); reintroduce; overload; virtual; deprecated;
    function GetItem(Index: Integer): T;
    procedure Notify(Item: T; Action: TCollectionNotification); reintroduce;
overload; virtual;
    procedure SetItem(Index: Integer; Value: T);
    procedure SetItemName(Item: T); reintroduce; overload; virtual;
    procedure Update(Item: T); reintroduce; overload; virtual;
  public
    type
      TCollectionEnumerator = class(Classes.TCollectionEnumerator)
      public
        function GetCurrent: T; inline;
        property Current: T read GetCurrent;
      end;


    constructor Create;
    function Add: T;
    function FindItemID(ID: Integer): T;
    function Insert(Index: Integer): T;
    function GetEnumerator: TCollection<T>.TCollectionEnumerator;
    property Items[Index: Integer]: T read GetItem write SetItem;
  end;


  TOwnedCollection<T: TCollectionItem> = class(TCollection<T>)
  private
    FOwner: TPersistent;
  protected
    function GetOwner: TPersistent; override;
  public
    constructor Create(AOwner: TPersistent);
  end;


implementation


 TCollection<T> 
constructor TCollection<T>.Create;
begin
  inherited Create(T);
end;


function TCollection<T>.Add: T;
begin
  Result := T(inherited Add);
end;


procedure TCollection<T>.Added(var Item: T);
begin
end;


procedure TCollection<T>.Added(var Item: TCollectionItem);
begin
  Added(T(Item));
end;


procedure TCollection<T>.Deleting(Item: T);
begin
end;


procedure TCollection<T>.Deleting(Item: TCollectionItem);
begin
  Deleting(T(Item));
end;


function TCollection<T>.FindItemID(ID: Integer): T;
begin
  Result := T(inherited FindItemID(ID));
end;


function TCollection<T>.GetEnumerator: TCollection<T>.TCollectionEnumerator;
begin
  Result := TCollection<T>.TCollectionEnumerator.Create(Self);
end;


function TCollection<T>.GetItem(Index: Integer): T;
begin
  Result := T(inherited GetItem(Index));
end;


function TCollection<T>.Insert(Index: Integer): T;
begin
  Result := T(inherited Insert(Index));
end;


procedure TCollection<T>.Notify(Item: T; Action: TCollectionNotification);
begin
  inherited Notify(Item, Action);
end;


procedure TCollection<T>.Notify(Item: TCollectionItem; Action: TCollectionNotification);
begin
  Notify(T(Item), Action);
end;


procedure TCollection<T>.SetItem(Index: Integer; Value: T);
begin
  inherited SetItem(Index, Value);
end;


procedure TCollection<T>.SetItemName(Item: T);
begin
  inherited SetItemName(Item);
end;


procedure TCollection<T>.SetItemName(Item: TCollectionItem);
begin
  SetItemName(T(Item));
end;


procedure TCollection<T>.Update(Item: T);
begin
  inherited Update(Item);
end;


procedure TCollection<T>.Update(Item: TCollectionItem);
begin
  Update(T(Item));
end;


 TCollection<T>.TCollectionEnumerator 
function TCollection<T>.TCollectionEnumerator.GetCurrent: T;
begin
  Result := T(inherited GetCurrent);
end;


 TOwnedCollection<T> 
constructor TOwnedCollection<T>.Create(AOwner: TPersistent);
begin
  FOwner := AOwner;
  inherited Create;
end;


function TOwnedCollection<T>.GetOwner: TPersistent;
begin
  Result := FOwner;
end;


end.

我不知道该代码的原作者(不是我),但我认为它是前一段时间在 Embarcadero 论坛上发布的。 类似的实现可以在DSharp.Core.Collections中找到

【讨论】:

以上是关于如何在泛型中实现 TCollection 和 TCollectionItem 功能的主要内容,如果未能解决你的问题,请参考以下文章

在泛型类中实现特定方法

如何在泛型类中获取Property和Value?

如何在泛型集合上创建扩展方法

C#,如何在泛型方法中访问对象方法

如何在泛型类型中使用 linq 包含函数

实现IComparable 在泛型中:我如何访问Generic的值?