0

I've an AsyncObservableCollection class:

public class AsyncObservableCollection<T> : ObservableCollection<T>
{
    private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;

    public AsyncObservableCollection()
    {
    }

    public AsyncObservableCollection(IEnumerable<T> list)
        : base(list)
    {
    }

    private void ExecuteOnSyncContext(Action action)
    {
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            action();
        }
        else
        {
            _synchronizationContext.Send(_ => action(), null);
        }
    }

    protected override void InsertItem(int index, T item)
    {
        ExecuteOnSyncContext(() => base.InsertItem(index, item));
    }

    protected override void RemoveItem(int index)
    {
        ExecuteOnSyncContext(() => base.RemoveItem(index));
    }

    protected override void SetItem(int index, T item)
    {
        ExecuteOnSyncContext(() => base.SetItem(index, item));
    }

    protected override void MoveItem(int oldIndex, int newIndex)
    {
        ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
    }

    protected override void ClearItems()
    {
        ExecuteOnSyncContext(() => base.ClearItems());
    }
}

I'm trying to add this metod:

 public static int Remove<T>(
    this ObservableCollection<T> coll, Func<T, bool> condition)
{
    var itemsToRemove = coll.Where(condition).ToList();

    foreach (var itemToRemove in itemsToRemove)
    {
        coll.Remove(itemToRemove);
    }

    return itemsToRemove.Count;
}

but I get this error:

extension method must be defined in a non-generic static class

I searched for this error and the solution was to change the class as static, but I already tried and didn't worked. any ideas?

Ilnumerouno
  • 65
  • 1
  • 6
  • 1
    You need to declare your extension method in a separate class, make that class static and not generic. – Ant P Oct 16 '17 at 10:40
  • But I've already inherit the ObservableCollection I can't inherit other classes.. – Ilnumerouno Oct 16 '17 at 10:41
  • Ant P is right on this – Big Daddy Oct 16 '17 at 10:42
  • You don't need inheritance for extension methods. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods - either use a new class or don't use an extension method (use a normal instance method). – Ant P Oct 16 '17 at 10:42
  • so if I'm right in the same file I need to create another class with extension method? could you show an example – Ilnumerouno Oct 16 '17 at 10:43

0 Answers0