tags:

views:

539

answers:

4

OK so that title sucks a little but I could not think of anything better (maybe someone else can?).

So I have a few questions around a subject here. What I want to do is create a program that can take an object and use reflection to list all its properties, methods, constructors etc. I can then manipulate these objects at runtime to test, debug and figure out exactly what some of my classes / programs are doing whilst they are running, (some of them will be windows services and maybe installed on the machine rather than running in debug from VS).

So I would provide a hook to the program that from the local machine (only) this program could get an instance of the main object and therefore see all the sub objects running in it. (for security the program may need to be started with an arg to expose that hook).

The "reflection machine" would allow for runtime manipulation and interrogation.

Does this sound possible?

Would the program have to provide a hook or could the "reflection machine" take an EXE and (if it knew all the classes it was using), create an object to use?

I know you can import DLL's at runtime so that it knows about all sorts of classes, but can you import individual classes? I.E. Say I have project 'Y' that is not compiled to a DLL but I want to use the "reflection machine" on it, can I point at that directory and grab the files to be able to reference those classes?

EDIT: I would love to try and develop it my self but I already have a long list of projects I would like to do and have already started. Why reinvent the wheel when there is already a great selection to choose from.

+4  A: 

Try looking at Crack.NET. It is used to do runtime manipulation and interrogation on WPF/WinForms but the source is available and might be a good start if it already doesn't meet your needs.

Kevin McMahon
I love Crack.Net, it looks wicked. This will be used in a work environment and we do not have 3.5 on the machines nor do we have ironpython, (they are not fans of putting extra bits in when they don't have to).
Jon
+1  A: 

Powershell actually does nearly all of this, if I properly understand what you are saying.

Doug McClean
Powershell looks interesting and worth exploring.
Jon
+2  A: 

It sound as if Corneliu Tusnea's Hawkeye might be close to what you're looking for runtime interrogation of objects/properties/etc. He calls it the .NET Runtime Object Editor. I'm not sure if the homepage I linked to above or the CodePlex project is the best place to start.

It's a bit out of date now, I think, but there's an earlier version of it on CodeProject where you can see the source code for how and what he did.

AR
Hawkeye looks great and could be ideal.
Jon
Can Hawkeye be used on command line apps. I have had a quick go with it and only seem to be able to hook into windows form apps.
Jon
I haven't used it extensively, but I think you're right that it can't hook into console apps.
AR
Thans AR. I think I will have a look at PowerShell or try and roll my own.
Jon
A: 

See this answer on how to build a "reflection engine".

All you need to do is to drop that set of machinery in the your set of available runtime libraries and it does what you want, I think. (It might not be as easy as I've made it sound in practice).

My guess is you'll also want a runtime compiler, so that you can manufacture instrumented/transformed variants of the program under inspection to collect the runtime data you want. You may find that such machinery provide static analysis results that let you avoid doing the runtime analysis in many cases.

Ira Baxter