7

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.

andrew.fox
  • 7,435
  • 5
  • 52
  • 75
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • 8
    You could use the code analysis tools to find unused methods instead, with the advantage of not changing your code (https://msdn.microsoft.com/en-us/library/3z0aeatx.aspx) – vc 74 Feb 13 '17 at 13:52
  • a free code analysis tool that can be runned only on specific classes? (anyway the code analysis tool should be triggered manually, we want the warning be outputted automatically) – CoffeDeveloper Feb 13 '17 at 13:53
  • @Everts actually we control each method by calling "find all references", but that's a tedious manual task that we want to automate :) thanks. – CoffeDeveloper Feb 13 '17 at 13:53
  • 4
    @DarioOO You can configure your projects so that code analysis is triggered after each build – vc 74 Feb 13 '17 at 13:54
  • @vc74 good, if you can show how to do that using a free analysis tool that would be accepted as answer since it would solve our problem :) (Do unity3D generated project files retain any custom configuration done to the project file?) – CoffeDeveloper Feb 13 '17 at 13:54
  • Problem in Unity is if you use UnityEvent as I said but also Invoke and InvokeRepeating as those are called via string so your method is not directly used. – Everts Feb 13 '17 at 13:55
  • @Everts we do not use unsafe events, actually we don't even use injection of properties from inspector :). The only exception are GUI buttons that anyway never calls directly audio trigger methods. Tjhanks for pointing out that anyway. – CoffeDeveloper Feb 13 '17 at 13:56
  • You can use static analyzers for this task. – Ivan Kishchenko Feb 21 '17 at 14:08

3 Answers3

2

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

Community
  • 1
  • 1
Pedro Luz
  • 973
  • 5
  • 14
2

A partial answer is found here:

C# reflection and finding all references

I can use that info to get references to methods marked with a particular attribute, however that is a run-time script (But better than nothing).

Community
  • 1
  • 1
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
0

A little late, but better than never: I use WinGrep by Gnu to search all folders and files for the name of the method:

`C:\>grep -irw "method_name" * --include=*.cs --include=*.sql --include=*.txt`

You can include as many, or as few, file name extensions as makes sense for you. In the example above, I show the top directory as C:, but you can start the search at any directory that makes sense.

The huge advantage of using grep over IDE based searches is that it will search across multiple projects and solutions.