IndexOf 的代码如下所示:
function TList<T>.IndexOf(const item: T; index, count: Integer): Integer;
begin
Result := TArray.IndexOf<T>(fItems, item, index, count, EqualityComparer);
end;
EqualityComparer 是一个调用 GetEqualityComparer 的属性,如下所示:
protected
class function GetEqualityComparer: IEqualityComparer<T>; static;
这static意味着该方法不能在子类中被覆盖。
您需要编辑 Spring 的源代码并进行以下更改:
Spring.Collections.Base
103: TEnumerableBase<T> = class abstract(TEnumerableBase, IEnumerable<T>)
106: protected
108: class var fEqualityComparer: IEqualityComparer<T>;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Move this line from private to the protected section.
.......
^^^^^^^^
现在您可以像这样创建自己的TCaseInsensitiveList:
type
TCaseInsensitiveList<T> = class(Spring.Collections.TList<T>)
constructor Create(const Comparer: IComparer<T>;
const EqualityComparer: IEqualityComparer<T>);
end;
.....
constructor TCaseInsensitiveList<T>.Create(
const Comparer: IComparer<T>;
const EqualityComparer: IEqualityComparer<T>);
begin
inherited Create(Comparer);
Self.fEqualityComparer:= EqualityComparer;
end;
或者,您可以将基GetEqualityComparer类函数声明为虚拟并在子类中覆盖它。
制作虚拟是有成本的GetEqualityComparer,这就是为什么我选择让底层类变量受保护的原因。