我正在尝试使用spring4d的集合部分。但我不能订阅集合变更事件。在以下位置获取错误: DCC error: E2008不兼容的类型:
var
TestList: TObjectList<TObject>;
begin
... List initialization code ...
TestList.OnNotify.Add(TestHandler); <--- Error here
endTObjectList的OnNotify属性声明为:
property OnNotify: ICollectionNotifyDelegate<T>,其中
ICollectionNotifyDelegate<T> = interface(IMulticastEvent<Generics.Collections.TCollectionNotifyEvent<T>>)
end;即OnNotify.Add方法需要一个Generics.Collections.TCollectionNotifyEvent,它声明为:
TCollectionNotifyEvent<T> = procedure(Sender: TObject; const Item: T;
Action: TCollectionNotification) of object;我的事件处理程序声明为:
procedure TTestClass.TestHandler(Sender: TObject; const Item: TObject; Action: TCollectionNotification);
begin
end;我很困惑%)请帮帮忙)
发布于 2012-02-06 00:17:37
这是由不同单位中的相同类型定义引起的:
Classes.pas:
TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
Generics.Collections.pas
TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);
实际上,Spring.Collections使用类型别名来简化使用:
TCollectionNotification = Generics.Collections.TCollectionNotification;
可以在uses list子句中的Classes后面添加Spring.Collections。
附注:
建议使用对接版本的IList<T>。
https://stackoverflow.com/questions/9150361
复制相似问题