118

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?

3
  • 2
    Sidenote: There is a generic constraint for parameterless constructors. Jan 13, 2011 at 14:30
  • 1
    is the question whether the type only provides a parameterless constructor or if it provides one at all? Jan 13, 2011 at 14:41
  • The latter. I don't mind the presence of additional ctors.
    – mafu
    Jan 13, 2011 at 16:32

9 Answers 9

205

The Type class is reflection. You can do:

Type theType = myobject.GetType(); // if you have an instance
// or
Type theType = typeof(MyObject); // if you know the type

var constructor = theType.GetConstructor(Type.EmptyTypes);

It will return null if a parameterless constructor does not exist.


If you also want to find private constructors, use the slightly longer:

var constructor = theType.GetConstructor(
  BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, 
  null, Type.EmptyTypes, null);

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.IsValueType property, and create instances using Activator.CreateInstance(Type);

0
20
type.GetConstructor(Type.EmptyTypes) != null

would fail for structs. Better to extend it:

public static bool HasDefaultConstructor(this Type t)
{
    return t.IsValueType || t.GetConstructor(Type.EmptyTypes) != null;
}

Succeeds since even enums have default parameterless constructor. Also slightly speeds up for value types since the reflection call is not made.

13

Yes, you have to use Reflection. But you already do that when you use GetType()

Something like:

var t = x.GetType();
var c = t.GetConstructor(new Type[0]);
if (c != null) ...
2
  • What does new "Type[0]" do? - It would be helpful to have that information inline. Jul 1, 2022 at 19:49
  • 1
    It's an empty array of Types. Same as Type.EmptyTypes. Jul 2, 2022 at 6:09
9

This should work:

   myClass.GetType().GetConstructors()
                    .All(c=>c.GetParameters().Length == 0)
3
  • 2
    Not what I meant, but please don't delete - it's a related issue and a nice information.
    – mafu
    Jan 13, 2011 at 16:34
  • 2
    Indeed, the correct expression for the OP's request should be myClass.GetType().GetConstructors().Any(c=>c.GetParameters().Length == 0). Note the use of Any instead of All. Mar 25, 2019 at 22:21
  • @GlennSlayden is correct. It should be .Any() instead of .All(). Otherwise the you will be checking if your type has only one constructor, that one constructor being parameter-less.
    – Cogent
    Oct 31, 2022 at 6:10
4

Depending on your situation, you could also use a generic type restriction:

public void DoSomethingWith<T>(T myObject) where T:new() {...}

The above method declaration will restrict the parameter type to any Object that can be instantiated with a parameterless constructor. The advantage here is that the compiler will catch any attempt to use the method with a class that doesn't have a parameterless constructor, so as long as the type is known SOMEWHERE at compile-time, this will work and will alert you to a problem earlier.

Of course if the type really is known only at runtime (i.e. you're using Activator.CreateInstance() to instantiate an object based on a string or a constructed Type) then this won't help you. I generally use reflection as the absolute last option, because once you've gone to dynamic land you pretty much have to stay in dynamic land; it's usually difficult or even messier to dynamically instantiate something and then start dealing with it statically.

1
  • Actually, there's a very useful pattern for bridging the lands of static and dynamic invocation: a static generic caching class [e.g. EqualityComparer.Default<T>]. For any given type T, one will only have to use Reflection once to build an object whose instance type will be statically known to qualify for any required constraints, and store a reference to it in a field that does not impose any constraints that a caller might not be able to supply.
    – supercat
    Apr 30, 2013 at 16:12
4

I needed to count constructors with only optional parameters the same as true parameter-less constructors. To do this:

myClass.GetType().GetConstructors()
    .All(c => c.GetParameters().Length == 0 || c.GetParameters().All(p => p.IsOptional))
4

If anyone is interested in an "official" version, the following was found via .NET Reflector:

from: System.Activities.Presentation.TypeUtilities
in System.Activities.Presentation.dll, Version=4.0.0.0

public static bool CanCreateInstanceUsingDefaultConstructor(this Type t) => 
                t.IsValueType || !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null;

Notice the check for t.IsAbstract, which is not mentioned elsewhere on this page.

You can also expand the GetConstructor call out as follows, if you feel like micro-optimizing away one stack frame:

…t.GetConstructor(BindingFlags.Public|BindingFlags.Instance, null, Type.EmptyTypes, null)…
1

Yes, you have to use reflection.

object myObject = new MyType();
Type type = myObject.GetType();
ConstructorInfo conInfo = type.GetConstructor(new Type[0]);
1

To get the one that has more optional parameters or an empty constructor at all, use:

    typeof(myClass)
    .GetConstructors()
    .OrderBy(x => x.GetParameters().Length - x.GetParameters().Count(p => p.IsOptional))
    .FirstOrDefault();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.