문제

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

  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$;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top