Given the following sample code;
class Program
{
static void Main(string[] args)
{
var results = GetChildren().ToList();
}
static IEnumerable<MyBaseClass> GetChildren()
{
return Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.BaseType == typeof(MyBaseClass))
.Select(o => (MyBaseClass)Activator.CreateInstance(o, null));
}
}
abstract class MyBaseClass
{ }
class MyChildClass : MyBaseClass
{
public MyChildClass(object paramOne)
{
}
}
I'm receiving the following error;
MissingMethodException: Constructor on type 'CreateInstanceCtorIssue.MyChildClass' not found.
However, if I add a parameterless constructor, it creates the objects OK.
I'm trying to work out why the parameter I'm suppying to CreateInstance is not causing it to find the correct constructor. Anyone got any ideas?
Activator.CreateInstance(o, (object)null)