web analytics

TypeLoadException is thrown when calling Type.GetType(String, Boolean) to get the Type with a specified name

Options

codeling 1595 - 6639
@2016-01-13 16:18:05

GetType Method (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.

The first parameter has to be an assembly-qualified name of the type to get such as "System.Exception". If you just input "Exception", the method will fail to get the Type with the name "Exception", and the second parameter controls whether to throw an exception.

The following example retrieves the type of System.Exception and uses that type object to display the FullName property of System.Exception . If a type object refers to an assembly that does not exist, this example throws an exception.

using System;
namespace MyTypeNameSpace
{
    class MyClass
    {
        public static void Main(string[] arg)
        {
            try
            {
                // Get the type of a specified class.

                Type myType1 = Type.GetType("System.Exception");
                Console.WriteLine("The full name is {0}.", myType1.FullName);


                // Since NoneSuch does not exist in this assembly,

                // GetType throws a TypeLoadException.

                Type myType2 = Type.GetType("Exception", true);
                Console.WriteLine("The full name is {0}.", myType2.FullName);

            }
            catch(TypeLoadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com