You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Constant evaluation of Nullable<T>.HasValue fails with System.Reflection.TargetException: Non-static method requires a target when null.
To Reproduce
Create a linq query with a Nullable<T> closure whose value is null, with a call to HasValue in it.
Expected behavior
The query would output true if the constant was non-null or false, if it was null.
Actual behavior
A System.Reflection.TargetException is thrown.
Environment summary
SDK Version: 3.27.0+
OS Version (e.g. Windows, Linux, MacOSX): OS independent
Additional context
This was introduced with #2924, which added a performance boost in evaluating constant members. This works well for most types, except for Nullable<T> which has special run-time behaviors that make it very difficult to use with reflection. Nearly all reflection calls use object as parameter (like PropertyInfo.GetValue(object obj)), which requires boxing of struct values. Nullable<T> have special behavior in the runtime where they the nullable itself is not boxed, but rather the value of it is. This results in passing null (rather than a Nullable<T> with no value) to PropertyInfo.GetValue, which throws the exception because it's an instance method without a valid target.
The text was updated successfully, but these errors were encountered:
Describe the bug
Constant evaluation of
Nullable<T>.HasValue
fails withSystem.Reflection.TargetException: Non-static method requires a target
whennull
.To Reproduce
Create a linq query with a
Nullable<T>
closure whose value isnull
, with a call toHasValue
in it.Expected behavior
The query would output
true
if the constant was non-null orfalse
, if it was null.Actual behavior
A
System.Reflection.TargetException
is thrown.Environment summary
SDK Version: 3.27.0+
OS Version (e.g. Windows, Linux, MacOSX): OS independent
Additional context
This was introduced with #2924, which added a performance boost in evaluating constant members. This works well for most types, except for
Nullable<T>
which has special run-time behaviors that make it very difficult to use with reflection. Nearly all reflection calls useobject
as parameter (likePropertyInfo.GetValue(object obj)
), which requires boxing of struct values.Nullable<T>
have special behavior in the runtime where they the nullable itself is not boxed, but rather the value of it is. This results in passingnull
(rather than aNullable<T>
with no value) toPropertyInfo.GetValue
, which throws the exception because it's an instance method without a valid target.The text was updated successfully, but these errors were encountered: