© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
11 replies
Hugh

✅ Using Activator.CreateInstance() when the constructor has arguments - not finding constructor

I'm trying to create a class instance dynamically using
Activator.CreateInstance()
Activator.CreateInstance()
, and it's crashing and not finding the constructor.

My object looks like this:
public class MyClass : MyBaseClass
{
    MyClass(ILogger logger) : base(logger)
    {}
}
public class MyClass : MyBaseClass
{
    MyClass(ILogger logger) : base(logger)
    {}
}


And I'm creating it like this:
Type myClassType = typeof(MyClass);
LoggerFactory loggerFactory = new();
ILogger logger = loggerFactory.CreateLogger(myClassType);
MyBaseClass? myInstance = (MyBaseClass?)Activator.CreateInstance(myClassType, logger);
Type myClassType = typeof(MyClass);
LoggerFactory loggerFactory = new();
ILogger logger = loggerFactory.CreateLogger(myClassType);
MyBaseClass? myInstance = (MyBaseClass?)Activator.CreateInstance(myClassType, logger);


However, when it hits the
CreateInstance()
CreateInstance()
line, an exception occurs:
An exception of type 'System.MissingMethodException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Constructor on type 'MyClass' not found.'
An exception of type 'System.MissingMethodException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Constructor on type 'MyClass' not found.'


Any idea what I'm doing wrong here?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Activator.CreateInstance Help!
C#CC# / help
15mo ago
✅ Pass in arguments to a generic constructor
C#CC# / help
2y ago
✅ Reflection cant find the constructor of a class that matches the given arguments
C#CC# / help
8mo ago
Is there any difference between named arguments and default constructor arguments for an attribute?
C#CC# / help
3y ago