I’d like to check if a type that is known at runtime provides a parameterless constructor. The Type class did not yield anything promising, so I’m assuming I have to use reflection?
I’d like to check if a type that is known at runtime provides a
Share
The
Typeclass is reflection. You can do:It will return null if a parameterless constructor does not exist.
If you also want to find private constructors, use the slightly longer:
There’s a caveat for value types, which aren’t allowed to have a default constructor. You can check if you have a value type using the
Type.IsValueTypeproperty, and create instances usingActivator.CreateInstance(Type);