Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Attribute to detect unused methods

Is it possible to write an Attribute that can track methods to detect if those methods are never called?

[Track]
void MyMethod(){

}

output:

warning: method "MyMethod" in "MyClass" has no references in code.

It is not strictly necessary to have it run at compile time, but it should work when the application is initialized (better at compile time anyway).

This tag will be putted to track methods on Audio Library, since audio is refactored very frequently and we usually search for audio methods with 0 references in code we want to mark these methods so we can detect quickly and remove unused audio assets.

Basically each time we add a new sound effect, we may later no longer trigger it (calling its method), and the audio file/playback code can remain in the application for a long time.

like image 202
CoffeDeveloper Avatar asked Feb 13 '17 13:02

CoffeDeveloper


1 Answers

Maybe this is the answer you're looking for?

Finding all references to a method with Roslyn

you can use the code there to automate something of your own with Reflection I'd say

like image 86
NotHere Avatar answered Sep 25 '22 19:09

NotHere