Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use reflection to create a "reflection machine"

Tags:

c#

reflection

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.

like image 630
Jon Avatar asked Nov 17 '08 23:11

Jon


3 Answers

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.

like image 195
Kevin McMahon Avatar answered Nov 13 '22 13:11

Kevin McMahon


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.

like image 41
AR. Avatar answered Nov 13 '22 13:11

AR.


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

like image 39
Doug McClean Avatar answered Nov 13 '22 14:11

Doug McClean