Consider this function:
static void Throw<T>(string message) where T : Exception
{
throw (T)Activator.CreateInstance(typeof(T), message, (Exception)null);
}
Given a type T
of System.ArgumentException
, as the question title says, I get a runtime error of "Ambiguous Match Found". Looking at the documentation for ArgumentException
, the following are the public constructors:
ArgumentException()
ArgumentException(string)
ArgumentException(SerializationInfo, StreamingContext)
ArgumentException(string, Exception)
ArgumentException(string, string)
ArgumentException(string, string, Exception)
Given I am passing 2 arguments in to CreateInstance
, and forcing the null
to be a null Exception
, I am struggling to understand why it is not matching the 4th constructor in the above list?