6

I have a C# class with various static public properties which work as keys.

Through time, this class is refactored many times and some keys are deprecated and no longer are referenced from other classes from projects in the same Visual Studio solution.

I want with reflection or some other way to check if any of these keys (properties) are used in other classes in the Visual Studio solution.

I want to write a unit test to discover if any keys are not used any more.

How I could do such thing?

  • 1
    This sounds like the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Perhaps try explaining what you actually want to do, and not how to do it. Also, a question on SO should provide a small, yet complete reproduce of the problem you're facing. – Yuval Itzchakov May 07 '15 at 09:08
  • you could right click the property, then select "find all references". CodeLens in visual studio should show you this information too – thumbmunkeys May 07 '15 at 09:08
  • If you have all the source-code then can't you just right-click on the property and choose 'Find all references' ? – Gerald Versluis May 07 '15 at 09:08
  • @thumbmunkeys He wants a *unit test* for that. – Yuval Itzchakov May 07 '15 at 09:09
  • 2
    And then lets hope that someone else isn't accessing a property only through reflection :-) For example, I don't know... a WPF project... – xanatos May 07 '15 at 09:09
  • @YuvalItzchakov _or some other way to check_ – thumbmunkeys May 07 '15 at 09:10
  • perhaps take a look at http://stackoverflow.com/help/how-to-ask first? – Amit May 07 '15 at 09:16
  • It is an interesting question what you are asking asked... I do think that being able to find dead methods/types/ can be useful (you could build some code analysis plugin with it) Sadly it is quite complex to do, and it is impossible to check for reflection. – xanatos May 07 '15 at 09:30
  • possible duplicate of - http://stackoverflow.com/questions/245963/find-unused-code At the moment you are not able to create this type of unit test. In the future you will be able to take advantage of Roslyn. – Pawel Maga May 07 '15 at 10:47
  • So it seems that it is not a simple task. I really dont want to use Roslyn just for writing one Unit Test. Isn't is possible with C# reflection? – Efstathios Chatzikyriakidis May 07 '15 at 13:57

1 Answers1

1

If you are not using reflection to access your propery/class, you can use Shift + F12 or right click on the property/class/field and choose 'Find all references'.

This will open up the 'Find symbol results' window where you can see all references to your code element.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
avb
  • 1,558
  • 1
  • 14
  • 37
  • 1
    He asked for a unit test, but the fact is that such a test would become useless after one or two runs. Far better to fix the issue simply and at source than have to try to hack a solution. – ZombieSheep May 07 '15 at 09:18
  • @ZombieSheep It is an interesting question what he asked... I do think that being able to find dead methods/types/ can be useful. Sadly it is quite complex to do, and it is impossible to check for reflection – xanatos May 07 '15 at 09:28
  • I really don't want to to use Visual Studio shortcuts but instead right a Unit Test in C#. I have done some code with reflection for doing nice things. But this seems hard problem. – Efstathios Chatzikyriakidis May 07 '15 at 09:56