What's the best way (in .NET 4) to create an instance of a type determined at runtime.
I have an instance method which although acting on a BaseClass object may be called by instances of its derived classes. I require to create another instance of the same type as this
within the method. Overloading the Method for each derived class is not practical as it is fairly involved and would be more efficient to keep to the single implementation.
public class BaseClass
{
//constructors + properties + methods etc
public SomeMethod()
{
//some code
DerivedClass d = new DerivedClass(); //ideally determine the DerivedClass type at run-time
}
}
I've read a bit about reflection or using the dynamic keyword but i don't have experience with these.
You are looking for
Activator.CreateInstance
(there are also other overloads, such as this one that accepts constructor arguments). So you could writeThere may be a problem here in that
anotherOneLikeMe
is going to be typed asobject
, so unless you intend to cast it to a common base class (e.g.BaseClass
in your example) there's not much that you can do with it.While indeed you need to use Activator.CreateInstance you might wanna look specifically into the Activator.CreateInstance(String, String) which can be invoked using the name of the class which you might know during runtime.
This will be mostly beneficial if you are instantiating Derived Types on the Base Type. If you're going to be calling
SomeMethod
from the derived type itself then the previous answers usingthis.GetType()
should be sufficient.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
:Make your method use a constrained generic type:
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.The problem here is you can never know the type of
DerivedClass
at compile time.You can however do this type of thing:
This is implemented like this:
This call will fail if the DerivedClass has no parameterless constructors though.
Best way for performance to repeatedly create instance in runtime is compiled expression:
Statistics (2012):
Statistics (2015, .net 4.5, x64):
Statistics (2015, .net 4.5, x86):
Full code:
This really depends on what you mean by "runtime" and what the goals are. For example, Jon and Bas have both hit on the idea of using Reflection to late bind a particular class and have it instantiated at Runtime. This is certainly one idea and you can even discover the methods on the object at runtime, if that is your goal
If you are using interfaces, you have a couple of additional options. The Microsoft Extensibility Framework (or MEF) might be an option you want to look at, as it includes discoverability and instantiation at runtime. The downside is the discovered class must adhere to the correct interface, but this is not a real issue in most cases.
If you know the classes you are loading, and they have the same interface (common theme), but want to instantiate different classes at runtime, IoC containers are an option. This is not particularly what you are asking about.
The dynamic keyword is not what you are looking for. It does load at runtime, but the dyanmic is more about the compiler not checking if the methods you are calling actually exist. Done incorrectly, you can end up with an interesting blowup when you call a method that does not exist. The main motivation I have seen for dyanamic is interaction with a dynamic language, like IronPython.