views:

208

answers:

9
+2  Q: 

Reflection of code

Is it possible to use reflection to know what the code is doing, not the types and methods, but rather what is inside the method (if statements, assignment and so on). I know I can disassemble it, but I need to analyze a class at runtime using C#, for example find out how many if conditions there are...

This tool needs to be in C#, any ideas? any classes in the CLR that can open an assembly and allow me to navigate the code within it.

+3  A: 

For sure reflection's purpose is not to expose such implementation details.

You'll need some decompiler/disassembler utility of sorts. Decompilers are typically targeted at one particular source language, so you need to have one that matches the language in which the assembly is written. Whether you'd be able to leverage the decompiler output or API at the level of your own C# programs is another story.

Furthermore beware that decompiling, and generally reverse engineering binaries and other supporting files may be illegal and in breach of licenses you may hold on the underlying software.

Reflector, mentioned above is effectively a decompiler for the .NET IL, and it uses reflection to allow interactive browsing of the assembly contents. This appears to be a stand-alone product however not something that would interface with a C# program.

mjv
A: 

You may want to look at the code around FxCop which has similar requirements. In particular, look at the introspection engine. An old but useful article from John Robbins: http://msdn.microsoft.com/en-us/magazine/cc163930.aspx

dpp
+1  A: 

May I suggest a static code analysis tool instead? You may not be able to get the number of "if" statements in an assembly, but you can get things like Cyclomatic complexity which is a similar measurement. Also some version of Visual Studio have code metrics built in. You could also make a Reflector Add-in to do that type of work.

JP Alioto
+1  A: 

If you have specific needs, as you mention "how many if conditions", you can do this by analyzing the IL. "IF" in IL translates fairly directly to brtrue and brfalse after the comparison is loaded. You may be able to get the metrics you're after by viewing the IL(at Runtime as requested).

To get started, I recommend checking out RAIL (Runtime Assembly Instrumentation Library). Linked is a sample application with mutate testing, but you'll see how you can quickly apply the code to get an "if" count which was your example.

If this doesn't fit, post some more examples of what you need to analyze the method body for.

Nick Craver
+3  A: 

Yes. The Microsoft FxCop tool uses an introspection model that is much richer than reflection. You can use the FxCop API to examine expressions in detail.

Wim Coenen
A: 

This article talks about compiling and decompiling a .NET assembly.

http://www.mactech.com/articles/mactech/Vol.19/19.12/NETbinaries/index.html

I have never had a need to do something like this, but your other option would be to unpack the assembly and write an application that can make sense of the bytecode and find the information you need.

James Black
A: 

As other commenters have said, FxCop is a tool that does this sort of analysis. But what you might be more interested in is the underlying technology it uses to do this: Common Compiler Infrastructure.

bobbymcr
A: 

http://www.ndepend.com/

csharptest.net
A: 

Reflection in a langauge only allows you to see what source code information the language designers thought was useful to keep, in most modern languages just function names, parameter types and classes.

If you want to do full code analysis, you always end up needing the source code. It doesn't make sense to store the source with the object code (frankly, if you had both, you'd just ditch the object code on the ground that's easy to get!).

Rather than insist that the langauge provide full access, you can simply step outside the langauge and get tools that can analyze the source code directly. Such tools can contain parsers for the full language, can build compiler data structures with useful facts ("who calls X?"), and make those facts available to your automated application.

One tool that can do this for many langauges (C, C++, Java, COBOL, C#, ...) is The DMS Software Reengineering Toolkit.

Ira Baxter