dotnet/wpf · error · ArgumentException

SR.KnownTypeIdNegative

Error message

SR.KnownTypeIdNegative

What it means

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.

Solutions

  1. Pass the negated (negative) id as stored in the BAML record; the method negates it back.
  2. Recompile the BAML/XAML if the stream may be corrupted by version skew.
  3. Guard with if (typeId < 0) before calling.
  4. Check you are not confusing BamlNumber with the encoded id sign convention.

Example fix

// before
var type = ctx.GetKnownBamlType(record.TypeId); // raw positive id
// after
var type = ctx.GetKnownBamlType((short)-record.TypeId); // BAML stores known-type ids negated
Defensive patterns

Strategy: validation

Validate before calling

if (typeId >= 0) throw new ArgumentException($"BAML type ids are stored negated; got {typeId}.");

Type guard

bool IsEncodedBamlTypeId(short id) => id < 0;

Try / catch

try { type = ctx.GetKnownBamlType(typeId); }
catch (ArgumentException) { /* log; fall back to reflection-based XamlType resolution */ }

Prevention

When it happens

Trigger: Calling GetKnownBamlType with a positive or zero typeId — e.g. passing a raw record value instead of the negated BAML-encoded known-type id.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedBamlSchemaContext.cs:122

                case 2: assembly = new Baml6Assembly(typeof(System.Windows.DependencyObject).Assembly); break;
                case 3: assembly = new Baml6Assembly(typeof(System.Windows.UIElement).Assembly); break;
                case 4: assembly = new Baml6Assembly(typeof(System.Windows.FrameworkElement).Assembly); break;
                default: assembly = null; break;
            }

            return assembly;
        }

        // Get Known TypesId
        // TypeId's defined in the stream are resolved by the Stream Schema Context
        //
        internal WpfKnownType GetKnownBamlType(short typeId)
        {
            WpfKnownType bamlType;

            if (typeId >= 0)
            {
                throw new ArgumentException(SR.KnownTypeIdNegative);
            }

            typeId = (short)-typeId;

            lock (_syncObject)
            {
                bamlType = _knownBamlTypes[typeId];
                if(bamlType == null)
                {
                    bamlType = CreateKnownBamlType(typeId, true, true);
                    Debug.Assert(bamlType != null);
                    _knownBamlTypes[typeId] = bamlType;
                    
                    _masterTypeTable.Add(bamlType.UnderlyingType, bamlType);
                }
            }
            return bamlType;
        }

View on GitHub (pinned to 81131a70a4)