unoplatform/uno · error · InvalidOperationException
Name must be set before calling ToString method.
Error message
Name must be set before calling ToString method.
What it means
Thrown by XamlTypeName.ToString(INamespacePrefixLookup) when the Name property is null. ToString needs both Namespace and Name to form a valid XAML type name string; a null Name produces a meaningless result, so the library throws InvalidOperationException.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeName.cs:243
TypeArguments = l;
}
}
public string Name { get; set; }
public string Namespace { get; set; }
public IList<XamlTypeName> TypeArguments { get; private set; }
public override string ToString ()
{
return ToString (null);
}
public string ToString (INamespacePrefixLookup prefixLookup)
{
if (Namespace == null)
throw new InvalidOperationException ("Namespace must be set before calling ToString method.");
if (Name == null)
throw new InvalidOperationException ("Name must be set before calling ToString method.");
string ret;
if (prefixLookup == null)
ret = String.Concat ("{", Namespace, "}", Name);
else {
string p = prefixLookup.LookupPrefix (Namespace);
if (p == null)
throw new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, "Could not lookup prefix for namespace '{0}'", Namespace));
ret = p.Length == 0 ? Name : p + ":" + Name;
}
string arr = null;
if (ret [ret.Length - 1] == ']') {
int idx = ret.LastIndexOf ('[');
arr = ret.Substring (idx);
ret = ret.Substring (0, idx);
}
if (TypeArguments.Count > 0)View on GitHub (pinned to 0418340488)
Solutions
- Ensure both Namespace and Name are non-null before calling ToString.
- Prefer the XamlTypeName(xamlNamespace, name) constructor which makes both required at construction time.
- Add a null check at the call site: if (!string.IsNullOrEmpty(tn.Name)) tn.ToString();
Example fix
// before
var tn = new XamlTypeName();
tn.Namespace = "http://example.com";
// Name never set
return tn.ToString();
// after
var tn = new XamlTypeName("http://example.com", nameFromData ?? throw new InvalidOperationException("name required"));
return tn.ToString(); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(tn.Namespace) || string.IsNullOrEmpty(tn.Name))
throw new InvalidOperationException("Namespace and Name required");
var s = tn.ToString(); Type guard
static bool IsComplete(XamlTypeName tn) => !string.IsNullOrEmpty(tn.Namespace) && !string.IsNullOrEmpty(tn.Name);
Try / catch
try { return tn.ToString(); }
catch (InvalidOperationException) { return fallbackName; } Prevention
- Use parameterized constructors that require both Namespace and Name.
- Validate both properties before any ToString call on default-constructed instances.
When it happens
Trigger: Calling ToString() on an XamlTypeName where Namespace is set but Name is still null. Typically from default constructor usage where only Namespace was assigned, or from new XamlTypeName("ns", null).
Common situations: Incremental construction of XamlTypeName objects where Name is loaded from async or optional data that arrived as null. Copying name fields from a source object whose Name was unset.
Related errors
- Namespace must be set before calling ToString method.
- Invalid typeName: '{0}'
- typeName
- namespaceResolver
- Invalid type name list: '{0}'
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/c2e4f83abe4d282a.
Report an issue: GitHub.