C#: How to check if a type has a default/parameterless constructor

The following C# snippet returns true if the passed in type has a default/parameterless constructor:


public bool HasDefaultConstructor(Type type)
{
	if (type.IsValueType)
		return true;

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

	if (constructor == null)
		return false;

	return true;
}
Published: 05.08.2010
blog comments powered by Disqus