开发者

How do I find all places that SET a property?

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 rea开发者_如何学Gods it.)


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.


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:

How do I find all places that SET a property?

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.


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


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 =.


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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜