-
Notifications
You must be signed in to change notification settings - Fork 4k
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
IDE0051 should not be reported when methods are referenced in nameof() expressions #31581
Comments
The diagnostic is by design, though the wording should be improved for this case - why would someone define a method only to use it's name? We should avoid offering a code fix for this case. |
Reflection. Our application server uses a framework that exposes some interfaces with methods whose parameters are either That might look something like this: // This is an implicit implementation of an interface method, thus we can't change the signature:
public bool DoSomething(ITypeInfo type, string id)
{
return (bool)typeof(ThisType)
.GetMethod(nameof(DoSomethingInternal), /* binding flags */)
.MakeGenericMethod(type.Type)
.Invoke(this, new object[] { id });
}
private void DoSomethingInternal<T>(string id)
{
return _queryService.Query<T>(id) // ...
} I'll admit, this is about the only case I can think of where you'd define a method only to use its name, but it's a bit odd to see IDE0051 complain about the method not being used, while the CodeLens info right above tells you "1 reference". |
@jhinder Thanks for the detailed explanation. I will fix this case to ensure we report IDE0052 with a different message for methods ( IDE0051 and IDE0052 diagnostics are not written to account for reflection based usage, you basically don't even need to have a name reference to access a member symbol via reflection. I would recommend the following for code bases that use reflection to access private members:
|
Version Used: VS 16.0 Preview 1
Steps to Reproduce:
Expected Behavior: IDE0051 is not reported for
M
.Actual Behavior: IDE0051 is reported for
M
. The code fix breaks any code that referencesM
.The text was updated successfully, but these errors were encountered: