dotnet/wpf · error · ArgumentException
SR.ParserKeysAreStrings (dictionary keys must be strings)
Error message
SR.ParserKeysAreStrings (dictionary keys must be strings)
What it means
The object indexer this[object prefix] throws this ArgumentException when the key is not a string. XmlnsDictionary is keyed by XML namespace prefixes, which are strings; the IDictionary object-based accessor enforces this by rejecting non-string keys (and null values on set) with the ParserKeysAreStrings message.
Solutions
- Cast the key to string (or call key.ToString()) before indexing
- Verify key type with 'is string' before lookup
Example fix
// before var ns = dictionary[key]; // key is object // after var ns = key is string s ? dictionary[s] : null;
Defensive patterns
Strategy: type-guard
Validate before calling
if (key is string p) { var ns = dictionary[p]; } Type guard
bool IsStringKey(object key) => key is string;
Try / catch
try { var ns = dictionary[key]; }
catch (ArgumentException) { /* key was not a string */ } Prevention
- Always index XmlnsDictionary with string keys
- Convert Uri/int keys to strings before indexing
- Restrict generic dictionary-walking code from touching XmlnsDictionary untyped
When it happens
Trigger: Reading dictionary[nonStringKey], e.g. dictionary[42] or dictionary[someUri].
Common situations: Generic code iterating IDictionary keys of mixed types; using a Uri object as the key instead of its string representation.
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
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.ChildHasWrongType (formatted with type name…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ed03db11acf2b57a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs:470
/// <summary>
/// A property indexing into the dictionary by XML prefix
/// </summary>
public string this[string prefix]
{
get { return LookupNamespace(prefix); }
set { AddNamespace(prefix, value as string);}
}
/// <summary>
/// A property indexing into the dictionary by XML prefix (supports objects to satisfy IDictionary spec)
/// </summary>
public object this[object prefix]
{
get
{
if (!(prefix is string))
{
throw new ArgumentException(SR.ParserKeysAreStrings);
}
return LookupNamespace((string)prefix);
}
set
{
if (!(prefix is string) || !(value is string))
{
throw new ArgumentException(SR.ParserKeysAreStrings);
}
AddNamespace((string)prefix, (string)value);
}
}
/// <summary>
/// An ICollection of all keys in the dictionary
/// </summary>
public ICollection Keys
{View on GitHub (pinned to 81131a70a4)