{"record":{"id":"b3ee2a37202889e8","repo":"devlooped/moq","slug":"type-0-does-not-have-a-default-public-parameterless","errorCode":null,"errorMessage":"Type {0} does not have a default (public parameterless) constructor.","messagePattern":"Type (.+?) does not have a default \\(public parameterless\\) constructor\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":24,"sourceCode":"using System.Diagnostics.CodeAnalysis;\nusing System.Globalization;\nusing System.Linq.Expressions;\nusing System.Reflection;\n\nusing Moq.Properties;\n\nusing TypeNameFormatter;\n\nnamespace Moq\n{\n    [DebuggerStepThrough]\n    static class Guard\n    {\n        public static void CanCreateInstance(Type type)\n        {\n            if (!type.CanCreateInstance())\n            {\n                throw new ArgumentException(\n                    string.Format(\n                        CultureInfo.CurrentCulture,\n                        Resources.TypeHasNoDefaultConstructor,\n                        type.GetFormattedName()));\n            }\n        }\n\n        public static void ImplementsInterface(Type interfaceType, Type type, string? paramName = null)\n        {\n            Debug.Assert(interfaceType != null);\n            Debug.Assert(interfaceType.IsInterface);\n\n            Debug.Assert(type != null);\n\n            if (interfaceType.IsAssignableFrom(type) == false)\n            {\n                throw new ArgumentException(\n                    string.Format(","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L6-L42","documentation":"Moq must instantiate an instance of the given type (e.g. for Mock.Of<T> with constructor arguments, Delegate mocks, or creating mock instances) and requires a public parameterless constructor. Guard.CanCreateInstance validates this up front and throws ArgumentException naming the type when no default constructor exists.","triggerScenarios":"Passing a type to an API that creates instances — e.g. new Mock<T> with constructor-argument-less instantiation paths, mock.Create, or guarded instantiation helpers — where T only has parameterized, private, internal, or no explicit constructors.","commonSituations":"Mocking classes with required constructor parameters (DI-injected services), types with private constructors (singletons, static-holder classes), or structs/classes without an accessible public default constructor.","solutions":["Add a public parameterless constructor to the type.","Provide the required constructor arguments via Mock<T>(object[] args) or use a factory instead of default instantiation.","Mock the interface the type implements instead of the concrete type.","Make the constructor public/accessible if it was private or internal by mistake."],"exampleFix":"// before\nclass Service { public Service(IDep dep) { ... } }\nvar mock = new Mock<Service>(); // throws\n// after\nvar mock = new Mock<Service>(new object[] { new Dep() });\n// or: var mock = new Mock<IService>();","handlingStrategy":"validation","validationCode":"bool canCreate = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null) != null;\nif (!canCreate) throw new InvalidOperationException($\"{type.Name} needs a public parameterless constructor or explicit args\");","typeGuard":"bool HasDefaultCtor(Type t) => t.IsInterface || t.GetConstructor(Type.EmptyTypes)?.IsPublic == true;","tryCatchPattern":"try { var mock = new Mock<Service>(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"default (public parameterless) constructor\")) { var mock = new Mock<Service>(new object[] { deps }); }","preventionTips":["Prefer mocking interfaces over concrete classes.","Provide constructor args via Mock<T>(object[]) when the type has required dependencies.","Avoid mocking types with private constructors (singletons).","Check Activator.CreateInstance(type) would succeed before mocking."],"tags":["moq","constructor","default-constructor"],"backgroundTag":"missing-default-constructor","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}