Delphi 如何使用OnNotify的通用TList

Delphi 如何使用OnNotify的通用TList,delphi,generics,delphi-xe5,Delphi,Generics,Delphi Xe5,我想使用通用TList的OnNotify事件。将过程分配给OnNotify会产生错误消息: E2010 Incompatible types: 'System.Generics.Collections.TCollectionNotification' and 'System.Classes.TCollectionNotification' 我声明了一个类,其中使用了一个通用TList,如下所示: TEditor_Table = class (TObject) public FEditors

我想使用通用TList的OnNotify事件。将过程分配给OnNotify会产生错误消息:

E2010 Incompatible types: 'System.Generics.Collections.TCollectionNotification' and 'System.Classes.TCollectionNotification'
我声明了一个类,其中使用了一个通用TList,如下所示:

TEditor_Table = class (TObject)
public
  FEditors: TList<TGradient_Editor>;  // List containing the editors
FColor_Editor := TEditor_Table.Create (List_Gradients);
FColor_Editor.FEditors.OnNotify := do_editor_change;
                                                   ^
error occurs here----------------------------------+
TColor_编辑器类的实例化如下:

TEditor_Table = class (TObject)
public
  FEditors: TList<TGradient_Editor>;  // List containing the editors
FColor_Editor := TEditor_Table.Create (List_Gradients);
FColor_Editor.FEditors.OnNotify := do_editor_change;
                                                   ^
error occurs here----------------------------------+

我一点也不理解这条消息,我也不明白为什么编译器会混淆这两个单元:“System.Generics.Collections.TCollectionNotification”和“System.Classes.TCollectionNotification”。我做错了什么?

问题是RTL定义了两个不同版本的
TCollectionNotification
。一个在
System.Classes
中,一个在
Generics.Collections

您正在使用
Generics.Collections中的
TList
,因此需要
Generics.Collections中的
TCollectionNotification
。但是在您的代码中,
TCollectionNotification
是在
System.Classes
中声明的版本。这是因为,在编写
TCollectionNotification
时,
System.class
是在
Generics.Collections
之后使用的

解决办法是:

  • 更改使用顺序,使
    泛型.Collections
    出现在
    系统.Classes
    之后。不管怎样,这都是一种良好的做法。或者
  • 完全指定类型:
    泛型.Collections.TCollectionNotification

  • 您遗漏了一个错误源:声明了System.Class而不是System.Generics.Collections(发生在本例中):-)您帮助我找到了罪魁祸首,谢谢!有一些严重的错误与这种混乱有关。我会听从你的建议,总是先声明System.Class。这不是错误的来源。如果省略了
    泛型.Collections
    ,则会出现不同的失败。那么您的失败是没有定义
    TList
    。在这种情况下是这样的。main表单中有一个
    system.classes
    声明,但没有导致编译器错误的
    system.generics.collection
    t列表
    在另一个单元中声明。添加system.generics.collection解决了错误。我看不到这些详细信息。顺便说一句,将FEditors公开似乎是错误的。代码真的是这样吗?