unoplatform/uno · error · XamlObjectWriterException

Specified static factory method '{0}' for type '{1}' was not

Error message

Specified static factory method '{0}' for type '{1}' was not found

What it means

Thrown when an object is constructed via x:Arguments combined with x:FactoryMethod and no matching static method is found. The writer searches public static methods (BindingFlags) of the underlying type whose name equals the factory method and whose parameter count equals the supplied arguments. No match yields this exception.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs:350

		[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "Types manipulated here have been marked earlier")]
		protected override void OnWriteEndMember ()
		{
			var xm = CurrentMember;
			var state = object_states.Peek ();

			if (xm == XamlLanguage.PositionalParameters) {
				var l = (List<object>) state.Value;
				state.Value = escaped_objects.Pop ();
				state.IsInstantiated = true;
				PopulateObject (true, l);
				return;
			} else if (xm == XamlLanguage.Arguments) {
				if (state.FactoryMethod != null) {
					var contents = (List<object>) state.Value;
					var mi = state.Type.UnderlyingType.GetMethods (static_flags).FirstOrDefault (mii => mii.Name == state.FactoryMethod && mii.GetParameters ().Length == contents.Count);
					if (mi == null)
						throw new XamlObjectWriterException (String.Format (CultureInfo.InvariantCulture, "Specified static factory method '{0}' for type '{1}' was not found", state.FactoryMethod, state.Type));
					state.Value = mi.Invoke (null, contents.ToArray ());
				}
				else
					PopulateObject (false, (List<object>) state.Value);
				state.IsInstantiated = true;
				escaped_objects.Pop ();
			} else if (xm == XamlLanguage.Initialization) {
				// ... and no need to do anything. The object value to pop *is* the return value.
			} else if (xm == XamlLanguage.Name || xm == state.Type.GetAliasedProperty (XamlLanguage.Name)) {
				string name = (string) CurrentMemberState.Value;
				name_scope.RegisterName (name, state.Value);
			} else {
				if (xm.IsEvent)
					SetEvent (xm, (string) CurrentMemberState.Value);
				else if (!xm.IsReadOnly) // exclude read-only object such as collection item.
					SetValue (xm, CurrentMemberState.Value);
			}
		}

View on GitHub (pinned to 0418340488)

Solutions

  1. Verify the static factory method exists with exactly the same number of parameters as x:Arguments children.
  2. Make the method `public static`.
  3. If no factory method is intended, remove x:FactoryMethod and let x:Arguments drive constructor selection.

Example fix

<!-- before -->
<Foo>
  <x:Arguments>
    <x:String>s</x:String>
  </x:Arguments>
  <x:FactoryMethod>Make</x:FactoryMethod>
</Foo>
<!-- after: add `public static Foo Make(string s)` to Foo -->
Defensive patterns

Strategy: validation

Validate before calling

// Before writing, confirm a public static method of the given name and arity exists.
var mi = typeof(Foo).GetMethod("Create", BindingFlags.Public | BindingFlags.Static)
    ?.GetParameters().Length == argCount ? /* ok */ : null;
if (mi == null)
    throw new InvalidOperationException("No matching static factory method found.");

Try / catch

try { writer.WriteStartObject(...); /* x:Arguments + FactoryMethod */ }
catch (XamlObjectWriterException ex) when (ex.Message.Contains("factory method")) {
    // verify the static factory method name, visibility, and parameter count.
}

Prevention

When it happens

Trigger: XAML using `<x:Arguments>` with `<x:FactoryMethod>Create</x:FactoryMethod>` where 'Create' does not exist on the type, is not static, is not public, or has a different parameter count than the arguments.

Common situations: Factory method renamed or removed; method is an instance method; visibility changed to internal/private; argument count in x:Arguments does not match any overload.

Related errors


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