I know this was marked as reflection, but I generally see reflection as a last resort for performance and complexity reasons. There are some instances where your design/usage requires reflection; however, I'll offer some alternatives for consideration:
Use a factory Func
:
public void SomeMethod(Func createDerived)
{
BaseClass d = createDerived();
}
Make your method use a constrained generic type:
public void SomeMethod() where TDerived : BaseClass, new()
{
TDerived d = new TDerived();
}
Under the covers this last alternative makes use of Activator.CreateInstance
as others have suggested. I prefer the last to reflection because they both require a parameter-less constructor, but the compiler enforces the constraint that the derived type must have a parameterless constructor whereas the reflection approach results in a runtime exception.