dotnet/wpf · error

Specified method is not supported.

Error message

Specified method is not supported.

What it means

MultipleCopiesCollection implements ICollection.CopyTo by throwing NotSupportedException. Because the collection is just one item repeated RepeatCount times, copying is deliberately unimplemented; consumers should enumerate it instead.

Solutions

  1. Enumerate with foreach / LINQ (Cast<object>().ToArray()) instead of CopyTo.
  2. Copy the collection bound to DataGrid.ItemsSource, which is a normal collection.
  3. Check IList.IsReadOnly/IsFixedSize before using CopyTo-based generic helpers.

Example fix

// before
((ICollection)presenter.ItemsSource).CopyTo(arr, 0);

// after
var arr = presenter.ItemsSource.Cast<object>().ToArray();
Defensive patterns

Strategy: fallback

Validate before calling

var items = itemsSource is IEnumerable e ? e.Cast<object>().ToArray() : Array.Empty<object>();

Type guard

bool SupportsCopyTo(object c) => c is ICollection col && !(c is System.Windows.Controls.MultipleCopiesCollection);

Try / catch

try { col.CopyTo(arr, 0); }
catch (NotSupportedException) { arr = col.Cast<object>().ToArray(); }

Prevention

When it happens

Trigger: Calling CopyTo(array, index) directly, or indirectly via APIs that snapshot an ICollection (some LINQ-to-ICollection helpers, serialization, design-time tooling) on the cells presenter ItemsSource.

Common situations: Copying the generated cells collection to an array for analysis or testing; code that drains any ICollection into an Array.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/086e3eb16d7503d8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MultipleCopiesCollection.cs:300

                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, RepeatCount);

                Debug.Assert(_item != null, "_item should be non-null.");
                return _item;
            }

            set
            {
                throw new InvalidOperationException();
            }
        }

        #endregion

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            throw new NotSupportedException();
        }

        public int Count
        {
            get { return RepeatCount; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return this; }
        }

        #endregion

View on GitHub (pinned to 81131a70a4)