{"record":{"id":"1147acb1856ccff3","repo":"dotnet/wpf","slug":"sr-unknownreferenceserialize","errorCode":null,"errorMessage":"SR.UnknownReferenceSerialize","messagePattern":"SR\\.UnknownReferenceSerialize","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileReference.cs","lineNumber":105,"sourceCode":"        #region Persistence\n\n        /// <summary>Save to a stream</summary>\n        /// <param name=\"reference\">reference to save</param>\n        /// <param name=\"writer\">The BinaryWriter to persist this object to.  \n        /// This method will alter the stream pointer of the underlying stream as a side effect.\n        /// Passing null simply calculates how many bytes would be written.</param>\n        /// <returns>number of bytes written including any padding</returns>\n        internal static int Save(CompoundFileReference reference, BinaryWriter writer)\n        {\n            int bytes = 0;\n\n            // NOTE: Our RefComponentType must be written by our caller\n            bool calcOnly = (writer == null);\n\n            // what are we dealing with here?\n            CompoundFileStreamReference streamReference = reference as CompoundFileStreamReference;\n            if ((streamReference == null) && (!(reference is CompoundFileStorageReference)))\n                throw new ArgumentException(SR.UnknownReferenceSerialize, nameof(reference));\n\n            // first parse the path into strings\n            string[] segments = ContainerUtilities.ConvertBackSlashPathToStringArrayPath(reference.FullName);\n            int entries = segments.Length;\n\n            // write the count\n            if (!calcOnly)\n                writer.Write( entries );\n\n            bytes += ContainerUtilities.Int32Size;\n\n            // write the segments - if we are dealing with a stream entry, don't write the last \"segment\"\n            // because it is in fact a stream name\n            for (int i = 0; i < segments.Length - (streamReference == null ? 0 : 1); i++)\n            {\n                if (!calcOnly)\n                {\n                    writer.Write( (Int32)RefComponentType.Storage );","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileReference.cs#L87-L123","documentation":"CompoundFileReference.Save serializes a reference (path into the compound file) and accepts only CompoundFileStreamReference or CompoundFileStorageReference. Passing any other CompoundFileReference-derived type raises ArgumentException(SR.UnknownReferenceSerialize, nameof(reference)) because the writer does not know how to encode its RefComponentType.","triggerScenarios":"Calling Save (directly or via compound-file write paths) with a reference object that is neither a stream nor a storage reference — e.g. a custom subclass of CompoundFileReference or a wrong object passed where a reference was expected.","commonSituations":"Custom OPC/compound-file extensions adding new reference kinds without extending Save; refactoring that swapped in a different reference type; deserialization code creating a type Save doesn't recognize.","solutions":["Pass only CompoundFileStreamReference or CompoundFileStorageReference instances to Save.","If you added a custom reference type, extend the serialization logic to handle it (or convert it to a supported type first).","Check where the reference object was created — a mis-mapped factory can yield the wrong subclass.","Inspect reference.GetType() at the call site to identify the unexpected type."],"exampleFix":"// before: saving a custom reference type\nCompoundFileReference r = new MyCustomReference(path);\nfile.Save(r, writer);\n\n// after: convert to a supported reference type\nvar r = new CompoundFileStorageReference(path);\nfile.Save(r, writer);","handlingStrategy":"type-guard","validationCode":"bool IsSerializableReference(CompoundFileReference r) => r is CompoundFileStreamReference || r is CompoundFileStorageReference;","typeGuard":"bool IsStreamOrStorageRef(CompoundFileReference r) => r is CompoundFileStreamReference or CompoundFileStorageReference;","tryCatchPattern":"try { reference.Save(writer); } catch (ArgumentException ex) when (ex.ParamName == \"reference\") { ConvertToSupportedReferenceAndRetry(); }","preventionTips":["Only pass CompoundFileStreamReference/CompoundFileStorageReference to Save","Type-check reference objects before serialization","Extend Save explicitly if you add custom reference subclasses"],"tags":["argument","serialization","wpf","packaging"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}