dotnet/wpf · error
ArgumentNullException(nameof(writer))
Error message
ArgumentNullException(nameof(writer))
What it means
XamlBrushSerializer.SerializeOn validates its BinaryWriter argument and throws ArgumentNullException when it is null. It is reached via ConvertStringToCustomBinary during brush/color serialization, so a null writer indicates the caller misconfigured the serialization stream.
Solutions
- Pass a valid, non-null BinaryWriter backed by an open stream to the serializer
- Ensure the surrounding XAML serialization service created its stream before conversion
- For custom code, guard against null writer before calling the serializer
Example fix
// before XamlBrushSerializer.SerializeOn(null, "#FFFF0000"); // after using var ms = new MemoryStream(); using var writer = new BinaryWriter(ms); XamlBrushSerializer.SerializeOn(writer, "#FFFF0000");
Defensive patterns
Strategy: type-guard
Validate before calling
if (writer == null) throw new ArgumentNullException(nameof(writer));
Type guard
bool HasWriter(BinaryWriter w) => w != null;
Try / catch
try { XamlBrushSerializer.SerializeOn(writer, value); }
catch (ArgumentNullException ex) when (ex.ParamName == "writer") { /* initialize the stream/writer first */ } Prevention
- Always create the BinaryWriter (and its stream) before serializer calls
- Dispose streams only after serialization completes
- Null-check arguments in custom serializer plumbing
When it happens
Trigger: Calling XamlBrushSerializer.ConvertStringToCustomBinary (or the serializer pipeline) with a null BinaryWriter, e.g. supplying a null stream/StreamContext to the XAML serializer infrastructure.
Common situations: Custom XAML serialization tooling invoking the brush serializer directly; a null stream passed to XamlWriter/serializer service plumbing; reflection-based calls bypassing the writer setup.
Related errors
- ArgumentNullException(nameof(clrNamespace))
- ArgumentNullException(nameof(contentType))
- ArgumentNullException(nameof(loaderType))
- ArgumentNullException(nameof(newNamespace))
- ArgumentNullException(nameof(oldNamespace))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/02e715ff7e8ab4c5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlBrushSerializer.cs:97
/// </summary>
/// <remarks>
/// This is called ONLY from the Parser and is not a general public method.
/// </remarks>
public override object ConvertCustomBinaryToObject(
BinaryReader reader)
{
// ********* VERY IMPORTANT NOTE *****************
// If this method is changed, then BamlPropertyCustomRecord.GetCustomValue() needs
// to be correspondingly changed as well
// ********* VERY IMPORTANT NOTE *****************
return SolidColorBrush.DeserializeFrom(reader);
}
#else
private static bool SerializeOn(BinaryWriter writer, string stringValue)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
KnownColor knownColor = KnownColors.ColorStringToKnownColor(stringValue);
#if !PBTCOMPILER
// ***************** NOTE *****************
// This section under #if !PBTCOMPILER is not needed in XamlBrushSerializer.cs
// because XamlBrushSerializer.SerializeOn() is only compiled when PBTCOMPILER is set.
// If this code were tried to be compiled in XamlBrushSerializer.cs, it wouldn't compile
// becuase of missing definition of s_knownSolidColorBrushStringCache.
// This code is added in XamlBrushSerializer.cs nevertheless for maintaining consistency in the codebase
// between XamlBrushSerializer.SerializeOn() and SolidColorBrush.SerializeOn().
// ***************** NOTE *****************
lock (s_knownSolidColorBrushStringCache)
{
SolidColorBrush scb;
if (s_knownSolidColorBrushStringCache.ContainsValue(stringValue))
{
knownColor = KnownColors.ArgbStringToKnownColor(stringValue);View on GitHub (pinned to 81131a70a4)