Jon
Jon

Reputation: 15200

How to use reflection to create a "reflection machine"

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.

Upvotes: 12

Views: 2248

Answers (4)

Ira Baxter
Ira Baxter

Reputation: 95316

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.

Upvotes: 0

AR.
AR.

Reputation: 40525

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.

Upvotes: 3

Kevin McMahon
Kevin McMahon

Reputation: 2644

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.

Upvotes: 6

Doug McClean
Doug McClean

Reputation: 14485

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

Upvotes: 1

Related Questions