Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Activator.CreateInstance cannot find a constructor that matches

Your error

System.MissingMethodException: Constructor on type 'MyApp.Services.ReportBuilder' not found.1 more line it can show

Fix it fast

Most common cause, 1 of 4

The arguments do not match any public constructor

Line the arguments up with one public constructor, position by position and type by type, or add a constructor that accepts what the caller already holds.

Fix
// ReportBuilder has: public ReportBuilder(string title, int pageSize)
var builder = Activator.CreateInstance(typeof(ReportBuilder), "Monthly", 50);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

MissingMethodException: Constructor on type 'X' not found means Activator.CreateInstance found no public constructor that matches the arguments in number, order and type. List the type's constructors, pass arguments that fit one, use the BindingFlags overload for an internal or private constructor, and annotate the Type for trimming. Applies to .NET 10 and .NET 11.

  • Applies to .NET 11.0, .NET 10.0 (LTS)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print every instance constructor the type really has, public and non-public, next to the argument types you pass. The mismatch is usually obvious once both lists are side by side.

Run this first
foreach (var ctor in type.GetConstructors(
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
    Console.WriteLine($"{(ctor.IsPublic ? "public" : "non-public")} ({string.Join(", ", ctor.GetParameters().Select(p => p.ParameterType.Name))})");
}
Console.WriteLine("passing: " + string.Join(", ", args.Select(a => a?.GetType().Name ?? "null")));

Then find the line you see in this list and open its fix:

  1. 1The arguments do not match any public constructorThe type has a public constructor, but its parameter list differs from what you pass in count, order or type
  2. 2The constructor exists but is internal, protected or privateThe first-step list shows a non-public constructor with exactly the parameters you pass
  3. 3The code asks for a parameterless constructor the type does not haveThe message says No parameterless constructor defined, and the call is CreateInstance(type) or CreateInstance<T>()
  4. 4Trimming removed a constructor that is only reached by reflectionIt works under dotnet run but fails in the published app, and the project sets PublishTrimmed to true

What does the error look like?

  • System.MissingMethodException: Constructor on type 'MyApp.Services.ReportBuilder' not found.
  • System.MissingMethodException: No parameterless constructor defined for type 'MyApp.Services.ReportBuilder'.

Cause 1 of 4

The arguments do not match any public constructor

You seeThe type has a public constructor, but its parameter list differs from what you pass in count, order or type

Check

CreateInstance(Type, params object[]) looks for the public constructor that best matches the arguments. The API reference says the args array has to match the constructor's parameters in number, order and type, and throws MissingMethodException with "No matching public constructor was found" as the documented reason when none does.

Check
Console.WriteLine(type.GetConstructor(new[] { typeof(string), typeof(int) }) is null ? "no (string, int) constructor" : "found");

Fix

Line the arguments up with one public constructor, position by position and type by type, or add a constructor that accepts what the caller already holds.

Fix
// ReportBuilder has: public ReportBuilder(string title, int pageSize)
var builder = Activator.CreateInstance(typeof(ReportBuilder), "Monthly", 50);

Verify

CreateInstance returns an object, and GetType().Name prints the expected type.

Verify
Console.WriteLine(builder?.GetType().Name);

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 4

The constructor exists but is internal, protected or private

You seeThe first-step list shows a non-public constructor with exactly the parameters you pass

Check

The short overloads only consider public constructors. With the BindingFlags overload, a value of zero also searches public constructors only, so a non-public one stays invisible until you ask for it with BindingFlags.NonPublic.

Check
Console.WriteLine(type.GetConstructors().Length + " public constructors");

Fix

Call the overload that takes BindingFlags and include NonPublic and Instance. For a parameterless constructor, CreateInstance(type, nonPublic: true) does the same.

Fix
var builder = Activator.CreateInstance(
    typeof(ReportBuilder),
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
    binder: null,
    args: new object[] { "Monthly", 50 },
    culture: null);

// parameterless and non-public:
var empty = Activator.CreateInstance(typeof(ReportBuilder), nonPublic: true);

Verify

The instance is created without changing the constructor's access modifier.

Verify
Console.WriteLine(builder is ReportBuilder);

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

The code asks for a parameterless constructor the type does not have

You seeThe message says No parameterless constructor defined, and the call is CreateInstance(type) or CreateInstance<T>()

Check

CreateInstance(Type) and the generic CreateInstance<T>() only use a parameterless constructor. As soon as a class declares any constructor with parameters, the compiler stops adding the empty one, so a factory or plugin loader that worked before breaks after someone adds a dependency to the constructor.

Check
Console.WriteLine(type.GetConstructor(Type.EmptyTypes) is null ? "no parameterless constructor" : "ok");

Fix

Either pass the constructor's arguments through the args overload, or add a public parameterless constructor if the type can be built without them.

Fix
public class ReportBuilder
{
    public ReportBuilder() : this("Untitled", 50) { }
    public ReportBuilder(string title, int pageSize) { /* ... */ }
}

Verify

Activator.CreateInstance<ReportBuilder>() returns an instance.

Verify
Console.WriteLine(Activator.CreateInstance<ReportBuilder>() is not null);

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

Trimming removed a constructor that is only reached by reflection

You seeIt works under dotnet run but fails in the published app, and the project sets PublishTrimmed to true

Check

A trimmed self-contained publish drops code the build cannot see being used. Microsoft warns that this analysis can cause failures at runtime, mostly around reflection, and a constructor called only through Activator.CreateInstance(Type) is a typical victim. The build prints trim warnings for the call when the Type is not annotated.

Check
dotnet publish -c Release -r linux-x64 -p:PublishTrimmed=true 2>&1 | grep -i "warning IL"

Fix

Annotate the Type parameter that flows into CreateInstance with DynamicallyAccessedMembers and PublicConstructors, so the trimmer keeps them. Where the type is known at compile time, a generic method with a new() constraint avoids reflection altogether.

Fix
static object Create(
    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
    => Activator.CreateInstance(type)!;

// or, with no reflection at all:
static T Create<T>() where T : new() => new T();

Verify

The publish output has no IL trim warnings for this call, and the published app creates the object.

Verify
dotnet publish -c Release -r linux-x64 -p:PublishTrimmed=true 2>&1 | grep -c "warning IL"

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Prefer generics with a new() constraint or a factory delegate over Activator.CreateInstance when the type is known at compile time.
  • Add a unit test that creates every plugin or handler type through the same reflection path the app uses, so a constructor change fails in CI.
  • Treat IL trim warnings as errors in projects that publish trimmed, so a missing annotation cannot reach production.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.