ReSharper highlights use of nameof with "Explicit argument passed to parameter with caller info attribute"
Asked Answered
D

2

36

I'm using the nameof function to get a property name as a string thus:

public bool IsRunning => ...;

...
RaisePropertyChanged(nameof(IsRunning));

ReSharper highlights this with the warning:

Explicit argument passed to parameter with caller info attribute

The code works, I was just wondering if the above warning is something I should worry about.

Doit answered 7/8, 2015 at 10:13 Comment(0)
S
28

was just wondering if the above warning is something I should worry about.

When you have CallerMemberName attribute attached, you don't have to explicitly pass a value, because the attribute will do exactly that for you. It will find the caller's name and use it, making your nameof declaration redundant. This is of course assuming you call RaisePropertyChanged from the actual property implementation.

ReSharper marks these calls as redundant when you explicitly pass a string literal. It should force the same logic with nameof as well.

Summerwood answered 7/8, 2015 at 10:17 Comment(5)
Ah I get it. Hadn't quite appreciated what CallerMemberName was doing.The RaisePropertyChanged function (part of ObservableObject in GalaSoft.MVVMLight) was written before nameof existed, I guess it would be different if written now.Doit
It's stupid because it doesn't check if name is different than current scope, so when I call explicitly to raise for another property it doesn't discover that.Louls
I agree Shimmy and therefore it is better to be explicit in most cases just to avoid problems in your codeLukas
How do I disable this warning? Can you please post an example? I don't have Re#er, but I'm working on a repo that its owner has and cares about this complaint.Louls
In a file, you can add at the top: // ReSharper disable ExplicitCallerInfoArgument And just once on the line above the call: // ReSharper disable once ExplicitCallerInfoArgumentPutrefy
G
4

Not as long as your code is called from the IsRunning property (which makes the warning valid. Specifying the property name would be redundant in that case). It doesn't seem you are doing that.

The warning just tells you that the RaisePropertyChanged has the CallerMemberNameAttribute set on the property, which it should. It is safe to ignore.

Gielgud answered 7/8, 2015 at 10:17 Comment(2)
Ah I see its nothing to do with nameof, its the RaisePropertyChanged that is being highlightedDoit
No, I think you understand the use I guess. (If not sure, read here) You just raise the event it has changed. If it did, you are okay.Gielgud

© 2022 - 2024 — McMap. All rights reserved.