BaseCodeTraverser visiting

Topics: Metadata Model, Source Model
Aug 5, 2009 at 9:29 PM

How comprehensive is this supposed to be, out of the box?

I've just written an override of BaseCodeTraverser to explore the structure of an assembly (in particular to find references to types/methods/fields defined outside that assembly), and I'm finding that some of the Visit methods aren't being called when I expect them to.

For example:

  • Visit(IMethodCall) doesn't call Visit(methodCall.MethodToCall), which means that Visit(IMethodReference) and Visit(ITypeMemberReference) don't get called.
  • Visit(IMethodDefinition) doesn't call Visit(method.Body), which means that Visit(IMethodBody) doesn't get called.
  • Visit(IMethodBody) must be overridden in order to provide an ISourceMethodBody to Visit; I'm not sure why BaseCodeTraverser isn't using SourceMethodBodyProvider to do this automatically (since you can't really traverse code without it).

There are probably more; these are just the ones I've noticed thus far.  They're all easy to work around, of course; I'm just surprised that they're not standard.

Aug 6, 2009 at 6:21 AM

These are good points. If you run into more of them please let me know.

As for your particular scenario: why not use the Metadata model? That will give you all of the references (if you overrride Visit(IMethodDefinition) to call Visit(IMethodBody)) and not involve the expense of decompiling the assembly into a Code Model.

Aug 6, 2009 at 9:47 AM

Assuming you mean BaseMetadataTraverser, I think I tried that at first, but it didn't work.  (This was before I discovered the missing definition->body link.)  Then I just assumed that it needed to decompile the code in order to evaluate it, even though that didn't work at first either. :)

I'll give that another try, thanks.

Aug 6, 2009 at 2:24 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.