Finding unused enum members

隐身守侯 提交于 2021-02-05 07:09:48

问题


What is the quickest way to determine which members of an enum are not being used?


回答1:


If you're using ReSharper, click on the enum to check, hit Alt+F7 (Shift+F12 if you're using VS shortcuts), and it'll give you a list of every place it's used in your entire solution.




回答2:


Comment the enum members out one by one and see if your code compiles. If compilation breaks, the member is used. Although this method only catches compile-time use of your enums. They could be used at runtime.




回答3:


Being on the safe side you can mark your members with ObsoleteAttribute. Adding [Obsolete(true)] will fail the build if given member is used.

This can obviously be used not only for enums but for nearly anything in .NET.

Resharper is your tool of choice if you need to delete the members from a solution and you're not worried about another uses in different solutions.




回答4:


Using find references on each member of the enum is the fastest way I can think of.




回答5:


Ctrl-F and search the entire namespace/project for that member of enum




回答6:


If you are using VS2005/8 Ctrl-Shift-F so search in the files. This will give you a list of files that you can double-click to goto the lines.

If you don't use VS then you can use WinGrep which will do the same thing (without the double-click feature)




回答7:


Commenting / uncommenting members. If the compiler does not throw an error the enum member is not used.

Update: As mentioned in the comments this of course only works for projects contained in the solution / the active build configuration. The same holds for the Find References and Ctrl+F methods.

Otherwise there is also the option to do a file search, e.g. using grep. However, this option only allows to do a string-based search and does not resolve any types.



来源:https://stackoverflow.com/questions/749004/finding-unused-enum-members

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!