简体   繁体   English

清理不需要的代码 C#

[英]Clean up unwanted code C#

I got a C# application with Visual Studio 2005. The code has some 300 KLOC, which has been abused over 3 years.我用 Visual Studio 2005 得到了一个 C# 应用程序。代码有大约 300 KLOC,已被滥用超过 3 年。

When I was going through code found out that lot of unused functions/methods/properties.当我浏览代码时,发现了很多未使用的函数/方法/属性。 Its not possible to clean the code manually (Requires checking each member and if found no references delete it.)手动清理代码是不可能的(需要检查每个成员,如果发现没有引用将其删除。)

I am looking to automate this process, by VS macro, which will walk through each member in code, if it does not have any references it should delete it, if found any references, check its calling member's references if calling member, does not have any reference it should delete both and so on.我希望通过 VS 宏自动化这个过程,它将遍历代码中的每个成员,如果它没有任何引用,它应该删除它,如果找到任何引用,检查它的调用成员的引用,如果调用成员没有任何引用都应该删除,依此类推。

I am sure some one would have cracked it earlier.我敢肯定有人会更早地破解它。

Resharper has a Clean Code function and gives pretty good indication of which methods/classes aren't being used. Resharper有一个干净的代码 function 并很好地指示了哪些方法/类没有被使用。

You can query your code base with NDepend using CQL to find out which methods and classes are not being used.您可以使用NDepend使用 CQL 查询您的代码库,以找出哪些方法和类没有被使用。

Be careful of code that is invoked via reflection.注意通过反射调用的代码。 A lot of refactoring tools will flag this code as not being accessed when in fact they are.许多重构工具会将此代码标记为未被访问,而实际上它们是。

The safest is to run your unit tests (you do have these already right?) before and after the refactoring to ensure that everything still works.最安全的是在重构之前和之后运行你的单元测试(你已经做对了吗?),以确保一切仍然有效。

1st use re-sharper as other have suggested.第一次使用 re-sharper 正如其他人建议的那样。

Resharper assumes public methods are used somewhere by an external assembly, even if they are not, so search and replace "public " with "private " and recompile. Resharper 假定外部程序集在某处使用了公共方法,即使它们不是,所以搜索并将“public”替换为“private”并重新编译。

I have used Gendarme (like FxCop) to determine which code wasn't called.我使用宪兵(如 FxCop)来确定哪些代码没有被调用。 AFAIK it can't be automated, but at least you don't need to go line by line. AFAIK 它不能自动化,但至少你不需要逐行 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM