-
Notifications
You must be signed in to change notification settings - Fork 752
Add null checks before binding #2406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
internal static bool ShouldBindMethod(MethodBase mb) | ||
{ | ||
return (mb.IsPublic || mb.IsFamily || mb.IsFamilyOrAssembly); | ||
} | ||
internal static bool ShouldBindMethod(MethodBase mb) => | ||
mb != null && | ||
(mb.IsPublic || mb.IsFamily || mb.IsFamilyOrAssembly); | ||
|
||
internal static bool ShouldBindField(FieldInfo fi) | ||
{ | ||
return (fi.IsPublic || fi.IsFamily || fi.IsFamilyOrAssembly); | ||
} | ||
internal static bool ShouldBindField(FieldInfo fi) => | ||
fi != null && | ||
(fi.IsPublic || fi.IsFamily || fi.IsFamilyOrAssembly); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this just mask issues? I don't remember if we have nullability annotations on, but I think if you want to add a check here, it should throw ArgumentNullException
, and instead null
should be handled at the call site.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does mask issues, but in a non-failing way which is a lot better than raising during startup (which effectively renders this library useless). This is an adaptation of what fixes #2405. Throwing an exception here is (apart from having a slightly better stacktrace) effectively the same as leaving the code unchanged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean I suggest to check for null
at the call site which is what #2405 (comment) does, and optionally add an argument check in ShouldBindMethod
to throw something better than NullReferenceException
.
Simply returning false
for null
inputs is error-prone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree, but I will also not put any more work into this. If you want to propose a different solution (in code), be my guest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Superceded by #2409 |
No description provided.