unoplatform/uno · error · ArgumentNullException

context

Error message

context

What it means

ArgumentNullException thrown by NameReferenceConverter.ConvertFrom (System.Xaml, ported from Mono) when the ITypeDescriptorContext argument is null. The converter needs the context to obtain IXamlNameProvider and to decide convertibility, so a null context is rejected with parameter name 'context'.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Windows.Markup/NameReferenceConverter.cs:54

			return sourceType == typeof (string);
		}

		public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
		{
			if (context == null)
			{
				return false;
			}

			var p = context.GetService (typeof (IXamlNameProvider)) as IXamlNameProvider;
			return p != null && destinationType == typeof (string);
		}

		public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (context == null)
			{
				throw new ArgumentNullException ("context");
			}

			var s = value as string;
			if (String.IsNullOrEmpty (s))
			{
				throw new InvalidOperationException ("Value must be non-null string.");
			}

			return s;
		}

		public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (context == null)
			{
				return null;
			}

View on GitHub (pinned to 0418340488)

Solutions

  1. Pass a non-null ITypeDescriptorContext obtained from the type-descriptor pipeline.
  2. In tests, build a minimal context whose GetService returns IXamlNameProvider.
  3. Null-check at the call site and supply a real context before converting.

Example fix

// before
var s = (string)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "Foo");

// after
var s = (string)converter.ConvertFrom(descriptorContext, CultureInfo.InvariantCulture, "Foo");
Defensive patterns

Strategy: validation

Validate before calling

static string Convert(NameReferenceConverter c, ITypeDescriptorContext ctx, string value) { if (ctx is null) throw new ArgumentNullException(nameof(ctx)); return (string)c.ConvertFrom(ctx, CultureInfo.InvariantCulture, value); }

Type guard

static bool HasContext(ITypeDescriptorContext ctx) => ctx?.GetService(typeof(IXamlNameProvider)) is not null;

Try / catch

try { return (string)converter.ConvertFrom(ctx, culture, value); } catch (ArgumentNullException ex) when (ex.ParamName == "context") { throw new InvalidOperationException("NameReferenceConverter requires a type-descriptor context."); }

Prevention

When it happens

Trigger: Calling ConvertFrom(null, culture, value) directly — outside a designer/type-descriptor pipeline that normally provides the context.

Common situations: Programmatic conversion in tests or custom hosting that omits the descriptor context, or a code path that drops the context before invoking the converter.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/48f67fe5e539bf53. Report an issue: GitHub.