{"record":{"id":"29132467b8067953","repo":"dotnet/wpf","slug":"sr-knowntypeidnegative","errorCode":null,"errorMessage":"SR.KnownTypeIdNegative","messagePattern":"SR\\.KnownTypeIdNegative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedBamlSchemaContext.cs","lineNumber":122,"sourceCode":"                case 2: assembly = new Baml6Assembly(typeof(System.Windows.DependencyObject).Assembly); break;\n                case 3: assembly = new Baml6Assembly(typeof(System.Windows.UIElement).Assembly); break;\n                case 4: assembly = new Baml6Assembly(typeof(System.Windows.FrameworkElement).Assembly); break;\n                default: assembly = null; break;\n            }\n\n            return assembly;\n        }\n\n        // Get Known TypesId\n        // TypeId's defined in the stream are resolved by the Stream Schema Context\n        //\n        internal WpfKnownType GetKnownBamlType(short typeId)\n        {\n            WpfKnownType bamlType;\n\n            if (typeId >= 0)\n            {\n                throw new ArgumentException(SR.KnownTypeIdNegative);\n            }\n\n            typeId = (short)-typeId;\n\n            lock (_syncObject)\n            {\n                bamlType = _knownBamlTypes[typeId];\n                if(bamlType == null)\n                {\n                    bamlType = CreateKnownBamlType(typeId, true, true);\n                    Debug.Assert(bamlType != null);\n                    _knownBamlTypes[typeId] = bamlType;\n                    \n                    _masterTypeTable.Add(bamlType.UnderlyingType, bamlType);\n                }\n            }\n            return bamlType;\n        }","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedBamlSchemaContext.cs#L104-L140","documentation":"GetKnownBamlType resolves negated BAML type ids to cached WpfKnownType instances, under a lock. Type ids in BAML are stored negative; a typeId >= 0 means the caller passed an unencoded/invalid id, so an ArgumentException with SR.KnownTypeIdNegative is thrown.","triggerScenarios":"Calling GetKnownBamlType with a positive or zero typeId — e.g. passing a raw record value instead of the negated BAML-encoded known-type id.","commonSituations":"Custom BAML readers or profilers decoding records and forgetting the negation convention; corrupted BAML from mismatched assembly versions; reflection-based tests calling the internal API with positive ids.","solutions":["Pass the negated (negative) id as stored in the BAML record; the method negates it back.","Recompile the BAML/XAML if the stream may be corrupted by version skew.","Guard with if (typeId < 0) before calling.","Check you are not confusing BamlNumber with the encoded id sign convention."],"exampleFix":"// before\nvar type = ctx.GetKnownBamlType(record.TypeId); // raw positive id\n// after\nvar type = ctx.GetKnownBamlType((short)-record.TypeId); // BAML stores known-type ids negated","handlingStrategy":"validation","validationCode":"if (typeId >= 0) throw new ArgumentException($\"BAML type ids are stored negated; got {typeId}.\");","typeGuard":"bool IsEncodedBamlTypeId(short id) => id < 0;","tryCatchPattern":"try { type = ctx.GetKnownBamlType(typeId); }\ncatch (ArgumentException) { /* log; fall back to reflection-based XamlType resolution */ }","preventionTips":["Preserve the sign of ids read from BAML records.","Avoid confusing positive record type tags with negated known-type ids."],"tags":["wpf","baml","schema-context","argument"],"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"}