{"record":{"id":"44e2b240ee56a40d","repo":"dotnet/wpf","slug":"sr-nodefaultconstructor","errorCode":null,"errorMessage":"SR.NoDefaultConstructor","messagePattern":"SR\\.NoDefaultConstructor","errorType":"exception","errorClass":"MissingMethodException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs","lineNumber":314,"sourceCode":"\n                if (s_securityFailureWithCtorDelegate == ThreeValuedBool.NotSet)\n                {\n                    s_securityFailureWithCtorDelegate =\n                        ThreeValuedBool.False;\n                }\n\n                if (s_securityFailureWithCtorDelegate == ThreeValuedBool.True)\n                {\n                    return false;\n                }\n\n                Type underlyingType = type._xamlType.UnderlyingType.UnderlyingSystemType;\n                // Look up public ctors only, for equivalence with Activator.CreateInstance\n                ConstructorInfo tConstInfo = underlyingType.GetConstructor(Type.EmptyTypes);\n                if (tConstInfo is null)\n                {\n                    // Throwing MissingMethodException for equivalence with Activator.CreateInstance\n                    throw new MissingMethodException(SR.Format(SR.NoDefaultConstructor, underlyingType.FullName));\n                }\n\n                if ((tConstInfo.IsSecurityCritical && !tConstInfo.IsSecuritySafeCritical) ||\n                    (tConstInfo.Attributes & MethodAttributes.HasSecurity) == MethodAttributes.HasSecurity ||\n                    (underlyingType.Attributes & TypeAttributes.HasSecurity) == TypeAttributes.HasSecurity)\n                {\n                    // We don't want to bypass security checks for a critical or demanding ctor,\n                    // so just treat it as if it were non-public\n                    type._isPublic = ThreeValuedBool.False;\n                    return false;\n                }\n\n                IntPtr constPtr = tConstInfo.MethodHandle.GetFunctionPointer();\n                // This requires Reflection Permission\n                Action<object> ctorDelegate = ctorDelegate =\n                    (Action<object>)s_actionCtor.Invoke(new object[] { null, constPtr });\n                type._constructorDelegate = ctorDelegate;\n                return true;","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L296-L332","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nclass Widget { public Widget(int size) { ... } }\n// after\nclass Widget { public Widget() : this(1) { } public Widget(int size) { ... } }","handlingStrategy":"try-catch","validationCode":"bool ok = xamlType.UnderlyingType?.GetConstructor(Type.EmptyTypes) is not null;","typeGuard":"static bool HasDefaultCtor(XamlType t) => t.UnderlyingType?.GetConstructor(Type.EmptyTypes) is not null;","tryCatchPattern":"try { obj = invoker.CreateInstance(null); } catch (MissingMethodException ex) { /* no public parameterless ctor */ }","preventionTips":["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."],"tags":["xaml","reflection","missing-constructor","instantiation"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}