I've got some inherited code that has a tendency to pass objects around as interfaces (IFoo, for example) then, at arbitrary places in the code, spontaneously cast them to concrete implementations of those interfaces (say, MyConcreteFoo).
Here's a silly example:
public bool IsThisFooScaredOfMonkeys(IFoo foo)
{
if (foo is MyConcreteFoo)
{
return ((MyConcreteFoo)foo).BelievesMonkeysAreEvil;
}
return false;
}
What I'd like to do is write an NDepend CQL query to pick up these sorts of casts and give me a count per method, or per type, or anything really. Just something so I know where I can start focusing my efforts on getting rid of this particular brand of silliness, rather than sending my team spelunking through the code on a random hunt for casts...
Does anyone know if there's a way to do that? I'm guessing not (there can't be too many people out there who need that particular functionality) but I figured I'd ask here first... :-)
Of course, any other ideas on ways to make the cast-hunting go faster would be equally appreciated.
This would be very nice, but NDepend is limited to a set of entities which does not cover individual statements.
NDepend Entities
Despite this limitation NDepend is still pretty awesome! Perhaps this is a version next feature.
Now Patrick Smacchia might be able to tell me different, so I would contact him with this question. I would expect to get a response back quickly as he is pretty on top of things.
If you are using ReSharper 5.0 it has a Structural Search that would allow you to find statements like this. You would have to build the search yourself, but it is a pretty powerful tool.
This pattern would catch the example above:
if($fooObject$ is $concreteFoo$)
{
return (($concreteFoo$)$fooObject$).$anyIdentifier$;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With