Type.GetType
throws exception for all invalid element types
Type.GetType(String) now throws a TypeLoadException for all types with an invalid element type, including byref-of-byref. Previously, this method returned null
for some corner cases.
Previous behavior
Type.GetType(String) threw a TypeLoadException for most types with an invalid element type, except a few corner cases such as byref-of-byref. For example, the following code returned null
in .NET 7:
Type.GetType("System.Object&&")
New behavior
Type.GetType(String) throws a TypeLoadException for all types with an invalid element type, including byref-of-byref. For example, the following code (which returned null
in .NET 7) throws an exception in .NET 8:
Type.GetType("System.Object&&")
Version introduced
.NET 8
Type of breaking change
This change is a behavioral change.
Reason for change
.NET had multiple type-name parsers, and it was not unusual for them to have different behavior in corner cases like this one. The behavior was unified on:
- If the type with the given name is not found, return
null
. - If the type is invalid, throw TypeLoadException. "Invalid" types include types with generic constraint violations or invalid composition of parameter types.
Recommended action
If your code relied on a null
return value for these corner cases, change it to catch a TypeLoadException instead.
Affected APIs
- GetType(String)
- GetType(String, Boolean)
- GetType(String, Boolean, Boolean)
- GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)
- GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)
- GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for