dotnet/wpf · error · ArgumentException

SR.Format(SR.Collection_BadDestArray, "Visual3DCollection")

Error message

SR.Format(SR.Collection_BadDestArray, "Visual3DCollection")

What it means

Visual3DCollection.CopyTo stores elements via Array.SetValue inside a try/catch; an element that cannot be cast to the destination array's element type raises InvalidCastException, which is re-thrown as ArgumentException(SR.Collection_BadDestArray, e). The destination array's element type is not assignment-compatible with Visual3D.

Solutions

  1. Copy into a Visual3D[] (the collection's exact element type) and cast elements individually afterward
  2. If copying to T[] where T : Visual3D, filter first: collection.OfType<T>().ToArray()
  3. Guard with typeof(Visual3D).IsAssignableFrom(destArrayType) before calling CopyTo

Example fix

// before
var arr = new ModelVisual3D[collection.Count];
collection.CopyTo(arr, 0); // throws if any child is not ModelVisual3D
// after
var arr = collection.OfType<ModelVisual3D>().ToArray();
Defensive patterns

Strategy: type-guard

Validate before calling

if (dest is Visual3D[] v3 && dest.Length >= collection.Count) collection.CopyTo(v3, 0);

Type guard

static Visual3D[] SafeSnapshot(Visual3DCollection c) { var a = new Visual3D[c.Count]; c.CopyTo(a, 0); return a; }

Try / catch

try { collection.CopyTo(arr, 0); } catch (ArgumentException e) { /* arr element type incompatible with Visual3D */ }

Prevention

When it happens

Trigger: Copying into an array typed as an incompatible element type (e.g. int[], or ModelVisual3D[] when the collection holds other Visual3D subclasses) so that Array.SetValue throws InvalidCastException.

Common situations: Copying a collection of mixed Visual3D subclasses into a T[] where T is one specific subclass; reflection-based copy utilities that create a wrongly-typed array.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Visual3DCollection.cs:174

            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.Collection_BadRank);
            }

            // Elsewhere in the collection we throw an AE when the type is
            // bad so we do it here as well to be consistent
            try
            {
                int count = _collection.Count;
                for (int i = 0; i < count; i++)
                {
                    array.SetValue(_collection[i], index + i);
                }
            }
            catch (InvalidCastException e)
            {
                throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, "Visual3DCollection"), e);
            }
        }

        /// <summary>
        ///     Determines if the list contains "value"
        /// </summary>
        public bool Contains(Visual3D value)
        {
            VerifyAPIReadOnly(value);

            return (value != null && (value.InternalVisualParent == _owner));
        }

        /// <summary>
        ///     Returns the index of value in the list
        /// </summary>
        public int IndexOf(Visual3D value)
        {

View on GitHub (pinned to 81131a70a4)