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)
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho