'Incorrectly' highlighted as unused

We have some C# code where Resharper is incorrectly (albeit understandably) indicating that methods are unused when they aren't.

 

The methods are declared as private then accessed from the base class using dynamic casting:

Declaration (derived class):

Usage (base class):

Whilst I wouldn't consider this a bug it is a potential problem as it could lead to methods being removed during a refactor/analysis phase by a careless programmer. Is there anything we can do (other than making the methods public) to stop this? Ideally we'd like to put something on the base class so that the 'behaviour' is inherited.

Thanks.

 

0
2 comments

You could try adding a [UsedImplicitly] annotation to the methods, or a [UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] annotation to the base class.

Annotations in Source Code - Help | ReSharper

0
Avatar
Permanently deleted user

Hey, thanks for that, good to know and it will certainly help. Unfortunately that won't help with the base class because it doesn't declare Apply. It's a bit weird but the descendants implement multiple Apply() methods so we're doing a kind of runtime version of compile time binding.

So to extend my first post that descendant also has:

...and indeed a couple of others.

The base class can't declare multiple methods (they'd have the same signature (IMediaCommand, IWorker) and it doesn't know what descendants will implement. Hence the use of dynamic.

0

Please sign in to leave a comment.