dotnet/wpf · error · MissingMethodException
SR.NoDefaultConstructor
Error message
SR.NoDefaultConstructor
What it means
During XamlTypeInvoker.CreateInstance, EnsureConstructorDelegate throws MissingMethodException('NoDefaultConstructor') when the underlying CLR type has no public parameterless constructor. The message deliberately mirrors Activator.CreateInstance's MissingMethodException since the invoker only supports default-constructor activation for XAML object elements.
Solutions
- Add a public parameterless constructor to the type.
- Supply constructor arguments via x:Arguments in the XAML or pass non-empty arguments to CreateInstance so a matching ctor path is used.
- Catch MissingMethodException and substitute a factory or a different instantiation strategy.
- Add a TypeConverter on the type so the XAML reader can construct it from a string instead of invoking a ctor.
Example fix
// before
class Widget { public Widget(int size) { ... } }
// after
class Widget { public Widget() : this(1) { } public Widget(int size) { ... } } Defensive patterns
Strategy: try-catch
Validate before calling
bool ok = xamlType.UnderlyingType?.GetConstructor(Type.EmptyTypes) is not null;
Type guard
static bool HasDefaultCtor(XamlType t) => t.UnderlyingType?.GetConstructor(Type.EmptyTypes) is not null;
Try / catch
try { obj = invoker.CreateInstance(null); } catch (MissingMethodException ex) { /* no public parameterless ctor */ } Prevention
- Give XAML-instantiated types a public parameterless constructor.
- Use x:Arguments or a TypeConverter when only parameterized ctors exist.
- Warn in code review before removing a public default ctor from a XAML-used type.
- Keep ctors public, not just internal, for XAML object elements.
When it happens
Trigger: Calling XamlTypeInvoker.CreateInstance (with null or empty arguments path reaching DefaultCtorXamlActivator) on a XamlType whose UnderlyingType only defines parameterized constructors — e.g. a POCO with just MyType(int x) — or whose parameterless ctor is non-public.
Common situations: XAML object elements for types lacking a default ctor without a x:TypeArguments/constructor-argument directive; model classes with required-argument ctors used in XAML resources; types that had a default ctor removed in a refactor (version-upgrade break); non-public ctors after making a class stricter.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- Cannot get a XamlDeferringLoader from XamlValueConverter
- SR.Format(SR.MarkupExtensionBadStatic, typeNameForError is…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/44e2b240ee56a40d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs:314
if (s_securityFailureWithCtorDelegate == ThreeValuedBool.NotSet)
{
s_securityFailureWithCtorDelegate =
ThreeValuedBool.False;
}
if (s_securityFailureWithCtorDelegate == ThreeValuedBool.True)
{
return false;
}
Type underlyingType = type._xamlType.UnderlyingType.UnderlyingSystemType;
// Look up public ctors only, for equivalence with Activator.CreateInstance
ConstructorInfo tConstInfo = underlyingType.GetConstructor(Type.EmptyTypes);
if (tConstInfo is null)
{
// Throwing MissingMethodException for equivalence with Activator.CreateInstance
throw new MissingMethodException(SR.Format(SR.NoDefaultConstructor, underlyingType.FullName));
}
if ((tConstInfo.IsSecurityCritical && !tConstInfo.IsSecuritySafeCritical) ||
(tConstInfo.Attributes & MethodAttributes.HasSecurity) == MethodAttributes.HasSecurity ||
(underlyingType.Attributes & TypeAttributes.HasSecurity) == TypeAttributes.HasSecurity)
{
// We don't want to bypass security checks for a critical or demanding ctor,
// so just treat it as if it were non-public
type._isPublic = ThreeValuedBool.False;
return false;
}
IntPtr constPtr = tConstInfo.MethodHandle.GetFunctionPointer();
// This requires Reflection Permission
Action<object> ctorDelegate = ctorDelegate =
(Action<object>)s_actionCtor.Invoke(new object[] { null, constPtr });
type._constructorDelegate = ctorDelegate;
return true;View on GitHub (pinned to 81131a70a4)