Skip to content

TypeLoadException.TypeName no longer contains the fully qualified name of the type that caused the exception. #111059

@drieseng

Description

@drieseng

Description

The TypeName property of TypeLoadException is documented to contain the fully qualified name of the type that caused the exception.

However, this property returns an empty string when the TypeLoadException is thrown by at least the following methods in both .NET 8.0 and .NET 9.0:

  • System.Reflection.Assembly.GetType(string name, bool throwOnError)
  • System.Type.GetType(string name, bool throwOnError)

Reproduction Steps

Compile and run the following code:

using System;
using System.Reflection;

public class Program
{
    private static void Main()
    {
        // Get a reference to the assembly mscorlib.dll, which is always
        // loaded. (System.String is defined in mscorlib.)
        Assembly mscorlib = typeof(string).Assembly;

        try
        {
            Console.WriteLine("Attempting to load a type that does not exist in mscorlib.");
            // The boolean parameter causes an exception to be thrown if the
            // type is not found.
            Type myType = mscorlib.GetType("System.NonExistentType", true);
        }
        catch (TypeLoadException ex)
        {
            // Display the name of the type that was not found, and the
            // exception message.
            Console.WriteLine("TypeLoadException was caught. Type = '{0}'.",
                ex.TypeName);
            Console.WriteLine("Error Message = '{0}'", ex.Message);
        }
    }
}

Important:
This code fragment is taken from the doc page of TypeLoadException.TypeName:
https://learn.microsoft.com/en-us/dotnet/api/system.typeloadexception.typename?view=net-9.0

Expected behavior

The following text is written to the console / stdout:

Attempting to load a type that does not exist in mscorlib.
TypeLoadException was caught. Type = ''.
...

Actual behavior

The following text is written to the console / stdout:

Attempting to load a type that does not exist in mscorlib.
TypeLoadException was caught. Type = 'System.NonExistentType'.
...

This is the output that you get on .NET Framework 4.8 and this is also the expected output that is documented on the doc page of TypeLoadException.TypeName:
https://learn.microsoft.com/en-us/dotnet/api/system.typeloadexception.typename?view=net-9.0

Regression?

This is a regression from the .NET Framework 4.8 implementation, and the .NET implementation also does not correspond with the doc page of TypeLoadException.TypeName:
https://learn.microsoft.com/en-us/dotnet/api/system.typeloadexception.typename?view=net-9.0

Known Workarounds

None

Configuration

  • .NET 8.0 or .NET 9.0, I haven't checked earlier versions.
  • Windows 11 version 23H2
  • X64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-TypeSystem-coreclrin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions