Home All Groups Group Topic Archive Search About

Reflecting on an executing assembly

Author
1 Mar 2007 4:20 PM
Tim Greenwood
I was wondering how to go about looking at an executing assembly and getting
a list of not all types but all instantiated types and references to those
objects.  Something similar to what the propertygrid in Visual Studio is
doing....I need to provide property grid like functionality for one of our
internal apps.

The snippet below shows what I've done which gives me a list of all the
types in the assembly...great.  Now how do I conjure up a reference to an
instance of one of these types?  I don't want to create a new one, I want to
find the ones already running so the users can see the runtime properties of
their UI.
---------------------------------------------------------------------------------
        private Type[] tArray;
        Module[] moduleArray;
        Module modCurrent;

        public propgrid()
        {
            InitializeComponent();
        }

        private void propgrid_Load(object sender, EventArgs e)
        {
            moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
            this.cboModules.DataSource = moduleArray;
        }

        private void cboModules_SelectedIndexChanged(object sender,
EventArgs e)
        {
            if (!cboModules.Focused) return;
            if (cboModules.SelectedIndex == null) return;
            modCurrent = moduleArray[cboModules.SelectedIndex];
            tArray = modCurrent.FindTypes(Module.FilterTypeName, "*");
            cboObjects.DataSource = tArray;
---------------------------------------------------------------------------------

Author
1 Mar 2007 4:25 PM
Jon Skeet [C# MVP]
<"Tim Greenwood" <tim_greenwood AT yahoo DOT com>> wrote:
> I was wondering how to go about looking at an executing assembly and getting
> a list of not all types but all instantiated types and references to those
> objects.

No, you can't do that, at least not without using a debugging/profiling
API.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
1 Mar 2007 5:09 PM
Tim Greenwood
So then how does Visual Studio do it?  Obviously the forms designer property
grid is showing us the properties of the objects it has instantiated....is
it reflecting? Or is it keeping an internal list of what it has created?

Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.20510edfe6b1051798d90a@msnews.microsoft.com...
> <"Tim Greenwood" <tim_greenwood AT yahoo DOT com>> wrote:
>> I was wondering how to go about looking at an executing assembly and
>> getting
>> a list of not all types but all instantiated types and references to
>> those
>> objects.
>
> No, you can't do that, at least not without using a debugging/profiling
> API.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Author
1 Mar 2007 8:21 PM
Jon Skeet [C# MVP]
<"Tim Greenwood" <tim_greenwood AT yahoo DOT com>> wrote:
> So then how does Visual Studio do it?  Obviously the forms designer property
> grid is showing us the properties of the objects it has instantiated....is
> it reflecting? Or is it keeping an internal list of what it has created?

Given that it's got to create the code to *recreate* the objects it's
created, yes, it keeps a list of things it's designing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

AddThis Social Bookmark Button