unoplatform/uno · error · ArgumentNullException

underlyingType

Error message

underlyingType

What it means

The XamlType(Type underlyingType, XamlSchemaContext, XamlTypeInvoker) constructor throws ArgumentNullException('underlyingType') when given a null Type. XamlType wraps a CLR type, so a null type leaves it with nothing to describe; the constructor also reads type.IsGenericType downstream, which would NRE otherwise.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlType.cs:55

	[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "Types manipulated here have been marked earlier")]
	[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "Types manipulated here have been marked earlier")]
	[UnconditionalSuppressMessage("Trimming", "IL2080", Justification = "Types manipulated here have been marked earlier")]
	public class XamlType : IEquatable<XamlType>
	{
		public XamlType (Type underlyingType, XamlSchemaContext schemaContext)
			: this (underlyingType, schemaContext, null)
		{
		}

//		static readonly Type [] predefined_types = {
//				typeof (XData), typeof (Uri), typeof (TimeSpan), typeof (PropertyDefinition), typeof (MemberDefinition), typeof (Reference)
//			};

		public XamlType (Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker)
			: this (schemaContext, invoker)
		{
			if (underlyingType == null)
				throw new ArgumentNullException ("underlyingType");
			type = underlyingType;
			underlying_type = type;

			XamlType xt;
			if (XamlLanguage.InitializingTypes) {
				// These are special. Only XamlLanguage members are with shorthand name.
				if (type == typeof (PropertyDefinition))
					Name = "Property";
				else if (type == typeof (MemberDefinition))
					Name = "Member";
				else
					Name = GetXamlName (type);
				PreferredXamlNamespace = XamlLanguage.Xaml2006Namespace;
			} else if ((xt = XamlLanguage.AllTypes.FirstOrDefault (t => t.UnderlyingType == type)) != null) {
				Name = xt.Name;
				PreferredXamlNamespace = XamlLanguage.Xaml2006Namespace;
			} else {
				Name = GetXamlName (type);

View on GitHub (pinned to 0418340488)

Solutions

  1. Resolve the Type up front and throw a descriptive error if null before constructing XamlType.
  2. Use Type.GetType with throwOnError=true (or Assembly.GetType and check) so a missing type fails loudly at the source.
  3. If the type is genuinely unknown, use the XamlType(string,string,IList<XamlType>,XamlSchemaContext) overload that represents an unknown type instead of the Type-based one.

Example fix

// before
var t = Type.GetType(typeName);
var xt = new XamlType(t, schemaContext, null);

// after
var t = Type.GetType(typeName, throwOnError: true);
var xt = new XamlType(t, schemaContext, null);
Defensive patterns

Strategy: validation

Validate before calling

if (underlyingType == null) throw new ArgumentNullException(nameof(underlyingType));
var xt = new XamlType(underlyingType, schemaContext, null);

Type guard

static XamlType? SafeXamlType(Type? t, XamlSchemaContext sctx)
    => t is null ? null : new XamlType(t, sctx, null);

Prevention

When it happens

Trigger: Constructing XamlType from a Type obtained via reflection that returned null (e.g. Type.GetType on a non-resolved name), or passing a nullable Type variable without checking.

Common situations: Reflection-based code that resolves types by string and silently returns null on failure; deserialization that builds XamlType from dynamically loaded types; partial/generics handling where GetType returns null for an open generic.

Related errors


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