views:

382

answers:

6

Is there a way to use reflection to completely "scan" an assembly to see if System.IO.File or System.IO.Directory is ever used? These are just example classes. Just wondering if there is a way to do it via reflection (vs code analysis).

update: see comments

A: 

You can get a list of dependent assemblies via Assembly.GetExecutingAssembly().GetReferencedAssemblies(). I don't believe you can comprehend namespace usage via reflection. Try looking at System.CodeDom. That may help you parse the code.

Adam Fyles
Well I'm not so interested that System.IO was used but that the class System.IO.File was used.
tyndall
I don't think just because an answer is probably wrong is justification for giving it a negative score. I upvoted to bring this to 0.
viggity
+1  A: 

I'd suggest looking at Mono Cecil for this. With Cecil, you can enumerate all the classes, methods and even the IL-instructions (including all the methods calls).

Tommy Carlier
I downloaded monocharge-20090414.tar.gz from the dailybuilds and do not see Mono.Cecil.dll. Is Mono.Cecil namespace in another assembly now?
tyndall
+1  A: 

I don't remember where, but I found this handy piece of code:

http://gist.github.com/raw/104001/5ed01ea8a3bf7c8ad669d836de48209048d02b96/MethodBaseRocks.cs

It adds an extension method to MethodInfo/ConstructorInfo that parses the ILByteArray into Instruction objects.

So with this, you could loop over every MethodInfo/ConstructorInfo in the assembly, then loop over every Instruction on that MethodInfo/ConstructorInfo, and check if any of those Instruction objects contains an Operand which is an instance of a MemberInfo which has a DeclaringType that is equal to either class.

Tinister
I think it comes from http://evain.net/blog/articles/2009/04/30/reflection-based-cil-reader
Jb Evain
A: 

.NET Reflector can do this, or something close to it. The other day I checked to see where a particular type was used.

ReSharper might also help. I do this with my own symbols all the time - I suppose it would also work for .NET Framework types.

John Saunders
+3  A: 

The fantastic NDepend tool will give you this sort of dependency information.

Load your dll in NDepend and either use the GUI to find what you want, or the following CQL query:

SELECT TYPES WHERE IsDirectlyUsing "System.IO.File"

and you should get a list of all the types that use this.

Rob Levine
hmmm I wonder how they are doing it under the hood. +1
tyndall
they use Mono.Cecil.
Jb Evain
you can also have indirect dependencies likeSELECT TYPES WHERE IsUsing "System.IO.File"and you'll get classes like A using B using C... using System.IO.File
Patrick Smacchia - NDepend dev
+6  A: 

As Tommy Carlier suggested, it's very easy to do with Cecil.

using Mono.Cecil;

// ..

var assembly = AssemblyFactory.GetAssembly ("Foo.Bar.dll");
var module = assembly.MainModule;

bool references_file = module.TypeReferences.Contains ("System.IO.File");
Jb Evain
One word. Awesome! Thanks.
tyndall
I downloaded monocharge-20090414.tar.gz from the dailybuilds and do not see Mono.Cecil.dll. Is Mono.Cecil namespace in another assembly now?
tyndall
It's in monocharge/1.0/Mono.Cecil.dll
Jb Evain
ah ha... ok. Thanks. Great project.
tyndall