44

It is easy to find all code that uses a property, however how do I find the code that just sets it?

(When I do a “find all reference” on the “set”, it just does a “find all reference” on the property itself, including code that just reads it.)

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • search through complete solution with ".PropertyName = " :) – Dimi Takis May 23 '11 at 10:51
  • Duplicate of http://stackoverflow.com/questions/5981149/is-there-a-better-way-to-find-all-references-to-a-property-setter – Ergwun Aug 05 '11 at 04:52
  • I really wish microsoft would just add a mechanism to the symbol searching to let you filter stuff out. Until then @Greco is correct; however, I would go one step further, try the following: `:b*=~(:Po|:Sm)`. It was helpful when I came across this: http://msdn.microsoft.com/en-us/library/2k3te2cs.aspx. Enjoy! – Rodolfo Neuber Oct 12 '11 at 21:12
  • Possible duplicate of [Is there a (better) way to find all references to a property setter?](http://stackoverflow.com/questions/5981149/is-there-a-better-way-to-find-all-references-to-a-property-setter) – Andy Jan 21 '17 at 02:41
  • 2
    See [Rosalyn issue #17684](https://github.com/dotnet/roslyn/issues/17684) to follow status of this. Recent comment says it has been implemented, though does not specify which forthcoming release is targeted to include it. Also see related [Rosalyn pull 30155](https://github.com/dotnet/roslyn/pull/30155) – ToolmakerSteve Oct 03 '18 at 21:29
  • I thought this was a feature in older versions of Visual Studio as I remember at one time having done this by accident and being surprised by the number of references being less than expected. – Matt Arnold Jan 11 '19 at 16:26

6 Answers6

56

You can use Resharper.

Alternately, set the setter to private (Or comment out the setter completely) and recompile. You will get errors where you're trying to set the property.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • 3
    I too like the compiler trick when ReSharper is not available. A step further would be to comment-out the setter entirely, so that even uses within the class will be surfaced by the compiler. – Allon Guralnek May 23 '11 at 10:59
  • 1
    http://stackoverflow.com/questions/5981149/is-there-a-better-way-to-find-all-references-to-a-property-setter#answer-5981271 – Jake Smith Apr 16 '16 at 17:48
  • 6
    Can you be please more specific related to the Resharper solution? – Szabolcs Antal Jun 23 '16 at 13:14
  • 10
    https://www.jetbrains.com/resharper/features/navigation_search.html After you bring up the Find Usages window in ReSharper, you can filter usages by "write usages" and "read usages". The screenshot under "Find Usages" section shows where they are. – Chun-Fu Chao Jul 19 '16 at 03:54
  • Yay for compilers! – RayLoveless Feb 03 '17 at 16:19
  • See my Sep 7'17 Answer below for a fairly robust solution that'll work also for non-Properties using Visual Studio without 3rd party tools. – Tom Sep 07 '17 at 18:21
  • @Chun-FuChao This should be the correct answer. The validated solution is really clumsy ... – StackHola Apr 13 '18 at 10:37
  • This is possible natively with VS2019: https://stackoverflow.com/a/53734123/9069356 – Dominus.Vobiscum Feb 05 '20 at 18:40
54

For what it's worth, this will be natively possible with VS2019.

Specifically the 'Find All References' window has a new 'Kind' column which can be filtered for 'Write' references:

enter image description here

The specific Github PR that added this feature is scheduled to be included in Visual Studio 2019 Preview 2 (16.0.P2) https://github.com/dotnet/roslyn/issues/22545

The full release of VS2019 is roadmapped for Q1 of 2019.

Johannes
  • 6,232
  • 9
  • 43
  • 59
20

Try commenting the set part of property and build it gives error at all the places where it is used.

Umesh CHILAKA
  • 1,466
  • 14
  • 25
1

You could run a text search on propertyName = - you can try using regex search to allow for 0 to n spaces between the name and =.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 5
    Not much help if you have a load of classes each of which has a property called PropertyName. – Dave Feb 20 '18 at 14:47
0

Here's a fairly robust solution that'll work also for non-Properties using Visual Studio without 3rd party tools. Be sure to select the "Match Case" and "Use Regular Expressions" options in Find.

1. For all except Post-/Pre-fix Increment and Shift Assignments:

  (^|[^\w.])MyVariable\s*([\+\-\*/%&|\^]|)=[\w\s]

2. For Post-/Pre-fix Increment and Shift Assignments:

  ((^|[^\w.])MyVariable\s*(\+\+|--)|(\+\+|--)\s*MyVariable[^\w.]|(^|[^\w.])MyVariable\s*(<<|>>)=)

3. For Out / Ref Parameters (N/A for Properties):

  (^|[^\w.])(out|ref)\s+MyVariable[^\w.]

CAVEATS:

  1. C#.NET only.
  2. Visual Studio 2012+ only.
  3. Does not work if "=" is followed by an EOL.
  4. Does not work if "MyVariable" is followed by an EOL.
  5. Depending on starting point and scope of the Find and scope of the Variable / Property, may find more / less references than necessary. When in doubt, error on side of "more", so you won't miss anything.
  6. Does not work for "."-prefixed Variables / Properties. 6.1. Unless you include it as part of the "MyVariable" (i.e. "MyStructVariable.MyStructField" or "MyObjectVariable.MyObjectField") but you risk finding too few references since there may be other Struct or Object Variables used to make Assignments to the same Struct or Object Field or Property.
Tom
  • 870
  • 11
  • 13
  • 1
    And the most important caveat: finds **all** "MyVariable"s; it obviously cannot distinguish between two variables with same name from different classes. One work-around is to temporarily **rename** the property you wish to search for, then do the search. – ToolmakerSteve Oct 03 '18 at 21:14
0

AFAIK, this can't be done using the standard features of Visual Studio - it doesn't do anything special for properties to check whether they are being used on the left or right side when searching, and, to be sure, there's no option to tell it to do so.

To give an option without having to run extra regexes or install other software, you could just browse through the results window to let your eyes scan for left-side occurrences - maybe not the most productive but I'm not sure I see a great advantage over other suggestions.

Lastly, @Kamyar's suggestion to make the properties no longer accessible does seem worth a look, but this depends on how long it takes your project to compile, it could take even longer to find'em all - I'm not sure why you'd need Resharper to do this though.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129