C# Type GetType(String, Boolean)

Description

Type GetType(String, Boolean) gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found.

Syntax

Type.GetType(String, Boolean) has the following syntax.


public static Type GetType(
  string typeName,
  bool throwOnError
)

Parameters

Type.GetType(String, Boolean) has the following parameters.

  • typeName - The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
  • throwOnError - true to throw an exception if the type cannot be found; false to return null. Specifying false also suppresses some other exception conditions, but not all of them. See the Exceptions section.

Returns

Type.GetType(String, Boolean) method returns The type with the specified name. If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. In some cases, an exception is thrown regardless of the value of throwOnError. See the Exceptions section.

Example

The following example retrieves the type of System.Int32 and uses that type object to display the FullName property of System.Int32.


/* w w w.  j a  va 2 s .  c o  m*/
using System;
namespace MyTypeNameSpace
{
    class MyClass
    {
        public static void Main(string[] arg)
        {
                Type myType1 = Type.GetType("System.Int32");
                Console.WriteLine("The full name is {0}.", myType1.FullName);
                Type myType2 = Type.GetType("NoneSuch", true);
                Console.WriteLine("The full name is {0}.", myType2.FullName);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version