Compartir a través de


Module.GetType Method (String, Boolean, Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Overridable Function GetType ( _
    className As String, _
    throwOnError As Boolean, _
    ignoreCase As Boolean _
) As Type
[ComVisibleAttribute(true)]
public virtual Type GetType(
    string className,
    bool throwOnError,
    bool ignoreCase
)

Parameters

  • className
    Type: System.String
    The name of the type to locate. The name must be fully qualified with the namespace.
  • throwOnError
    Type: System.Boolean
    true to throw an exception if the type cannot be found; false to return nulla null reference (Nothing in Visual Basic).
  • ignoreCase
    Type: System.Boolean
    true to perform a case-insensitive search; otherwise, false.

Return Value

Type: System.Type
The specified type, if the type is declared in this module; otherwise, nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentNullException

className is nulla null reference (Nothing in Visual Basic).

TargetInvocationException

The class initializers are invoked and an exception is thrown.

ArgumentException

className is a zero-length string.

TypeLoadException

throwOnError is true, and the type cannot be found.

FileNotFoundException

className requires a dependent assembly that could not be found.

FileLoadException

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.

BadImageFormatException

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.

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.

NoteNote:

If the type has been forwarded to another assembly, it is still returned by this method.

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.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetType does not throw ArgumentException when an empty string is passed as className. Instead, it returns nulla null reference (Nothing in Visual Basic).

Examples

The following example attempts to get a type that does not exist in the currently executing module. The throwOnError parameter is true, so an exception is thrown.

Imports System.Reflection

Class Example
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        Dim moduleArray() As [Module]

        moduleArray = [Assembly].GetExecutingAssembly().GetModules()

        Dim myModule As [Module] = moduleArray(0)

        Dim myType As Type

        Try
            myType = myModule.GetType("UnknownType", True, True)
            outputBlock.Text += String.Format("Found type: {0}" & vbLf, myType.ToString())
        Catch ex As Exception
            outputBlock.Text += String.Format("Caught {0}: {1}" & vbLf, _
                                              ex.GetType().Name, _
                                              ex.Message)
        End Try
    End Sub 
End Class 

' This example produces output similar to the following:
'
'Caught TypeLoadException: Could not load type 'UnknownType' from assembly 'SilverlightApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
using System;
using System.Reflection;

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Module[] moduleArray;

        moduleArray = Assembly.GetExecutingAssembly().GetModules();

        Module myModule = moduleArray[0];

        Type myType;

        try
        {
            myType = myModule.GetType("UnknownType", true, false);
            outputBlock.Text += String.Format("Found type: {0}\n", myType.ToString());
        }
        catch (Exception ex)
        {
            outputBlock.Text += String.Format("Caught {0}: {1}\n", 
                                              ex.GetType().Name, 
                                              ex.Message);
        }
    }
}

/* This example produces output similar to the following:

Caught TypeLoadException: Could not load type 'UnknownType' from assembly 'SilverlightApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.