开发者

Can I use NDepend to count casts?

开发者 https://www.devze.com 2023-01-13 00:58 出处:网络
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 conc开发者_高级运维rete imp

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 conc开发者_高级运维rete 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

  1. Methods
  2. Fields
  3. Types
  4. Namespaces
  5. Assemblies

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.

On A Side Note:

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$;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消