Module.GetType Method

Definition

Returns the specified type.

Overloads

GetType(String)

Returns the specified type, performing a case-sensitive search.

GetType(String, Boolean)

Returns the specified type, searching the module with the specified case sensitivity.

GetType(String, Boolean, Boolean)

Returns the specified type, specifying whether to make a case-sensitive search of the module and whether to throw an exception if the type cannot be found.

GetType(String)

Source:
Module.cs
Source:
Module.cs
Source:
Module.cs

Returns the specified type, performing a case-sensitive search.

C#
public virtual Type? GetType(string className);
C#
public virtual Type GetType(string className);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType(string className);

Parameters

className
String

The name of the type to locate. The name must be fully qualified with the namespace.

Returns

A Type object representing the given type, if the type is in this module; otherwise, null.

Attributes

Exceptions

className is null.

The class initializers are invoked and an exception is thrown.

className is a zero-length string.

className requires a dependent assembly that could not be found.

className requires a dependent assembly that was found but could not be loaded.

-or-

The current assembly was loaded into the reflection-only context, and className requires a dependent assembly that was not preloaded.

className requires a dependent assembly, but the file is not a valid assembly.

-or-

className requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

Examples

The following example displays the name of a type in the specified module.

C#
using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;

            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass");
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}

Remarks

Note

If the type has been forwarded to another assembly, it is still returned by this method. For information on type forwarding, see Type Forwarding in the Common Language Runtime.

A type can be retrieved from a specific module using Module.GetType. Calling Module.GetType on the module that contains the manifest will not search the entire assembly. To retrieve a type from an assembly, regardless of which module it is in, you must call Assembly.GetType.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1

GetType(String, Boolean)

Source:
Module.cs
Source:
Module.cs
Source:
Module.cs

Returns the specified type, searching the module with the specified case sensitivity.

C#
public virtual Type? GetType(string className, bool ignoreCase);
C#
public virtual Type GetType(string className, bool ignoreCase);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType(string className, bool ignoreCase);

Parameters

className
String

The name of the type to locate. The name must be fully qualified with the namespace.

ignoreCase
Boolean

true for case-insensitive search; otherwise, false.

Returns

A Type object representing the given type, if the type is in this module; otherwise, null.

Attributes

Exceptions

className is null.

The class initializers are invoked and an exception is thrown.

className is a zero-length string.

className requires a dependent assembly that could not be found.

className requires a dependent assembly that was found but could not be loaded.

-or-

The current assembly was loaded into the reflection-only context, and className requires a dependent assembly that was not preloaded.

className requires a dependent assembly, but the file is not a valid assembly.

-or-

className requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

Examples

The following example displays the name of a type in the specified module, specifying false for the ignoreCase parameter so that case will not be ignored.

C#
using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", false);
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}

Remarks

Note

If the type has been forwarded to another assembly, it is still returned by this method. For information on type forwarding, see Type Forwarding in the Common Language Runtime.

A type can be retrieved from a specific module using Module.GetType. Calling Module.GetType on the module that contains the manifest will not search the entire assembly. To retrieve a type from an assembly, regardless of which module it is in, you must call Assembly.GetType.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1

GetType(String, Boolean, Boolean)

Source:
Module.cs
Source:
Module.cs
Source:
Module.cs

Returns the specified type, specifying whether to make a case-sensitive search of the module and whether to throw an exception if the type cannot be found.

C#
public virtual Type GetType(string className, bool throwOnError, bool ignoreCase);
C#
public virtual Type? GetType(string className, bool throwOnError, bool ignoreCase);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType(string className, bool throwOnError, bool ignoreCase);

Parameters

className
String

The name of the type to locate. The name must be fully qualified with the namespace.

throwOnError
Boolean

true to throw an exception if the type cannot be found; false to return null.

ignoreCase
Boolean

true for case-insensitive search; otherwise, false.

Returns

A Type object representing the specified type, if the type is declared in this module; otherwise, null.

Attributes

Exceptions

className is null.

The class initializers are invoked and an exception is thrown.

className is a zero-length string.

throwOnError is true, and the type cannot be found.

className requires a dependent assembly that could not be found.

className requires a dependent assembly that was found but could not be loaded.

-or-

The current assembly was loaded into the reflection-only context, and className requires a dependent assembly that was not preloaded.

className requires a dependent assembly, but the file is not a valid assembly.

-or-

className requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

Examples

The following example displays the name of a type in the specified module. The throwOnError and ignoreCase parameters are specified as false.

C#
using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing this class.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", false, false);
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}

Remarks

The throwOnError parameter affects only what happens when the type is not found. It does not affect any other exceptions that might be thrown. In particular, if the type is found but cannot be loaded, TypeLoadException can be thrown even if throwOnError is false.

Note

If the type has been forwarded to another assembly, it is still returned by this method. For information on type forwarding, see Type Forwarding in the Common Language Runtime.

A type can be retrieved from a specific module using Module.GetType. Calling Module.GetType on the module that contains the manifest will not search the entire assembly. To retrieve a type from an assembly, regardless of which module it is in, you must call Assembly.GetType.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0