dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, value.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(DocumentReference))

What it means

DocumentSequence.AddChild implements IAddChild and only accepts DocumentReference children. If the value passed cannot be cast to DocumentReference, it throws ArgumentException with SR.UnexpectedParameterType. DocumentSequence markup/child collections must contain only <DocumentReference> elements.

Solutions

  1. Only add DocumentReference instances to a DocumentSequence
  2. In XAML, wrap each child FixedDocument in a <DocumentReference> element
  3. If building programmatically, create a DocumentReference, set its Source (or Id/Document), then call AddChild
  4. Move unrelated children (styles, resources) out of the FixedDocumentSequence element

Example fix

// before
fixedDocumentSequence.AddChild(fixedDocument);
// after
var docRef = new DocumentReference();
docRef.Document = fixedDocument;
fixedDocumentSequence.AddChild(docRef);
Defensive patterns

Strategy: type-guard

Type guard

static bool IsDocumentReference(object value) => value is DocumentReference;

Try / catch

if (docRef is DocumentReference r) fixedDocumentSequence.AddChild(r); else throw new ArgumentException(nameof(docRef));

Prevention

When it happens

Trigger: Calling AddChild directly with a non-DocumentReference object; putting an element other than DocumentReference inside a FixedDocumentSequence in XAML so the parser calls AddChild with the wrong type.

Common situations: Hand-editing XAML and nesting a FixedDocument or PageContent directly under FixedDocumentSequence instead of DocumentReference; programmatic document construction adding pages directly to the sequence.

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/15d29ab85b3e7ca4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs:101

        #region IAddChild

        ///<summary>
        /// Called to Add the object as a Child.
        ///</summary>
        ///<param name="value">
        /// Object to add as a child
        ///</param>
        void IAddChild.AddChild(Object value)
        {
            ArgumentNullException.ThrowIfNull(value);

//             Dispatcher.VerifyAccess();

            DocumentReference docRef = value as DocumentReference;

            if (docRef == null)
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(DocumentReference)), nameof(value));
            }

            if (docRef.IsInitialized)
            {
                _references.Add(docRef);
            }
            else
            {
                DocumentsTrace.FixedDocumentSequence.Content.Trace($"Doc {_references.Count} Deferred");
                if (_partialRef == null)
                {
                    _partialRef = docRef;
                    _partialRef.Initialized += new EventHandler(_OnDocumentReferenceInitialized);
                }
                else
                {
                    throw new InvalidOperationException(SR.PrevoiusUninitializedDocumentReferenceOutstanding);
                }

View on GitHub (pinned to 81131a70a4)