You need to provide the fully-qualified name, i.e. "System.NullReferenceException".
In this particular case, that's enough - because NullReferenceException
is in mscorlib. However, if you need other exceptions - such as ConstraintException
, which lives in the System.Data assembly - you'd need to provide the full assembly name too. Type.GetType(string)
only looks in the currently executing assembly and mscorlib for type names which don't specify the assembly.
EDIT: Here's a short but complete program which works:
using System;
using System.Reflection;
class Test
{
static void Main()
{
string typeName = "System.NullReferenceException";
string message = "This is a message";
Type type = Type.GetType(typeName);
ConstructorInfo ctor = type.GetConstructor(new[] { typeof(string) });
object exception = ctor.Invoke(new object[] { message });
Console.WriteLine(exception);
}
}