我有如下所示的代码。我遇到的问题是这部分代码给出了编译错误。
Changed(this, new ListChangedEventArgs(Operation.Add, e.Value, 1));
错误 1 使用泛型类型“CustomDatastructures.Core.ListChangedEventArgs”需要 1 个类型参数
第二个问题是如何调用 onChanged 方法。
//public delegate void ListChanged<TEventArgs>(object sender, TEventArgs e);
public delegate void BeforeListChanged<TEventArgs>(object sender, TEventArgs e);
public delegate void ListChanged<TEventArgs>(object sender, TEventArgs e);
// Make this class generic by adding a type-parameter to the class
public class ObservableList<T> : IEnumerable<T>
{
// Declare an private variabel to work as
// the internal data storage for the list
List<T> observerList = new List<T>();
public event ListChanged<ListChangedEventArgs<T>> Changed;
//public event BeforeListChanged<T> BeforeChanged;
protected virtual void OnChanged(object sender, Operation op, T value, int count)
{
if (Changed != null)
Changed(this, new ListChangedEventArgs(op, value, count));
}
/// <summary>
/// Add and object to the list
/// </summary>
/// <param name="item">An object</param>
public void Add(T item)
{
observerList.Add(item);
OnChanged(this, Operation.Add, item, 1);
}
这是 ListChangedEvent 类的定义
public class ListChangedEventArgs<T> : EventArgs
{
public int Count { get; }
public Operation Operation { get; }
public T Value { get; }
public ListChangedEventArgs(Operation operation, T value, int count)
}