By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,465 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,465 IT Pros & Developers. It's quick & easy.

Getting reference to running object

P: n/a
I need to inspect the current AppDomain for an object that implements a
certain interface and get a property from the object. I know how to find out
what class implements my interface but Reflection only lets me instantiate a
new object. I want to get the property value from the already running
object.

Thanks.

</joel>
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Joel,

You will have to implement a way to make the object discoverable then.
The best way to do this (since the scope is the app domain) is to have a
static method or property which will give you the object you want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I need to inspect the current AppDomain for an object that implements a
certain interface and get a property from the object. I know how to find
out what class implements my interface but Reflection only lets me
instantiate a new object. I want to get the property value from the already
running object.

Thanks.

</joel>

Nov 16 '05 #2

P: n/a
Nicholas,

But that's the problem. I don't know what object it is. I can use the
StackTrace to "walk up" the stack and find which class implements my
interface like this:

System.Diagnostics.StackTrace strace=new System.Diagnostics.StackTrace(1);
int c=strace.FrameCount;
for(int i=0; i<c; i++)
{
System.Diagnostics.StackFrame sframe=strace.GetFrame(i);
Type t=sframe.GetMethod().DeclaringType.GetInterface("I Wip");
if(t!=null)
{
PropertyInfo pi=t.GetProperty("IsWip");
}
}

but now I want to get a reference to the object that's running.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:um**************@TK2MSFTNGP12.phx.gbl...
Joel,

You will have to implement a way to make the object discoverable then.
The best way to do this (since the scope is the app domain) is to have a
static method or property which will give you the object you want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I need to inspect the current AppDomain for an object that implements a
certain interface and get a property from the object. I know how to find
out what class implements my interface but Reflection only lets me
instantiate a new object. I want to get the property value from the
already running object.

Thanks.

</joel>


Nov 16 '05 #3

P: n/a
Joel,

You can't do this. Reflection will not allow you to get arbitrary items
on the stack.

If you want access to this, you will have to hook into the CLR (through
unmanaged code through the profiling APIs, most likely).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

But that's the problem. I don't know what object it is. I can use the
StackTrace to "walk up" the stack and find which class implements my
interface like this:

System.Diagnostics.StackTrace strace=new System.Diagnostics.StackTrace(1);
int c=strace.FrameCount;
for(int i=0; i<c; i++)
{
System.Diagnostics.StackFrame sframe=strace.GetFrame(i);
Type t=sframe.GetMethod().DeclaringType.GetInterface("I Wip");
if(t!=null)
{
PropertyInfo pi=t.GetProperty("IsWip");
}
}

but now I want to get a reference to the object that's running.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:um**************@TK2MSFTNGP12.phx.gbl...
Joel,

You will have to implement a way to make the object discoverable then.
The best way to do this (since the scope is the app domain) is to have a
static method or property which will give you the object you want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I need to inspect the current AppDomain for an object that implements a
certain interface and get a property from the object. I know how to find
out what class implements my interface but Reflection only lets me
instantiate a new object. I want to get the property value from the
already running object.

Thanks.

</joel>



Nov 16 '05 #4

P: n/a
It doesn't have to be via the StackTrace, that was just a way I could walk
bakwards looking for the Interface implementation I wanted. Is there some
other way? Thanks for your help.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u8**************@TK2MSFTNGP12.phx.gbl...
Joel,

You can't do this. Reflection will not allow you to get arbitrary
items on the stack.

If you want access to this, you will have to hook into the CLR (through
unmanaged code through the profiling APIs, most likely).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

But that's the problem. I don't know what object it is. I can use the
StackTrace to "walk up" the stack and find which class implements my
interface like this:

System.Diagnostics.StackTrace strace=new
System.Diagnostics.StackTrace(1);
int c=strace.FrameCount;
for(int i=0; i<c; i++)
{
System.Diagnostics.StackFrame sframe=strace.GetFrame(i);
Type t=sframe.GetMethod().DeclaringType.GetInterface("I Wip");
if(t!=null)
{
PropertyInfo pi=t.GetProperty("IsWip");
}
}

but now I want to get a reference to the object that's running.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:um**************@TK2MSFTNGP12.phx.gbl...
Joel,

You will have to implement a way to make the object discoverable
then. The best way to do this (since the scope is the app domain) is to
have a static method or property which will give you the object you
want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I need to inspect the current AppDomain for an object that implements a
certain interface and get a property from the object. I know how to find
out what class implements my interface but Reflection only lets me
instantiate a new object. I want to get the property value from the
already running object.

Thanks.

</joel>



Nov 16 '05 #5

P: n/a
Joel,

Unfortunately, no, there is not. Unless you make the instance
discoverable by some means, your only option is to hook into the CLR through
the profiling API.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...
It doesn't have to be via the StackTrace, that was just a way I could walk
bakwards looking for the Interface implementation I wanted. Is there some
other way? Thanks for your help.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:u8**************@TK2MSFTNGP12.phx.gbl...
Joel,

You can't do this. Reflection will not allow you to get arbitrary
items on the stack.

If you want access to this, you will have to hook into the CLR
(through unmanaged code through the profiling APIs, most likely).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

But that's the problem. I don't know what object it is. I can use the
StackTrace to "walk up" the stack and find which class implements my
interface like this:

System.Diagnostics.StackTrace strace=new
System.Diagnostics.StackTrace(1);
int c=strace.FrameCount;
for(int i=0; i<c; i++)
{
System.Diagnostics.StackFrame sframe=strace.GetFrame(i);
Type t=sframe.GetMethod().DeclaringType.GetInterface("I Wip");
if(t!=null)
{
PropertyInfo pi=t.GetProperty("IsWip");
}
}

but now I want to get a reference to the object that's running.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:um**************@TK2MSFTNGP12.phx.gbl...
Joel,

You will have to implement a way to make the object discoverable
then. The best way to do this (since the scope is the app domain) is to
have a static method or property which will give you the object you
want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
>I need to inspect the current AppDomain for an object that implements a
>certain interface and get a property from the object. I know how to
>find out what class implements my interface but Reflection only lets me
>instantiate a new object. I want to get the property value from the
>already running object.
>
> Thanks.
>
> </joel>
>



Nov 16 '05 #6

P: n/a
Have the object's method insert a reference to the object into a data slot
in the current thread (and restore the previous value when the method
returns.) Yoiu can now find the object via Thread.GetData().
"Joel" <jo******@hotmail.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

But that's the problem. I don't know what object it is. I can use the
StackTrace to "walk up" the stack and find which class implements my
interface like this:

System.Diagnostics.StackTrace strace=new System.Diagnostics.StackTrace(1);
int c=strace.FrameCount;
for(int i=0; i<c; i++)
{
System.Diagnostics.StackFrame sframe=strace.GetFrame(i);
Type t=sframe.GetMethod().DeclaringType.GetInterface("I Wip");
if(t!=null)
{
PropertyInfo pi=t.GetProperty("IsWip");
}
}

but now I want to get a reference to the object that's running.

</joel>

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:um**************@TK2MSFTNGP12.phx.gbl...
Joel,

You will have to implement a way to make the object discoverable then.
The best way to do this (since the scope is the app domain) is to have a
static method or property which will give you the object you want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joel" <jo******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I need to inspect the current AppDomain for an object that implements a
certain interface and get a property from the object. I know how to find
out what class implements my interface but Reflection only lets me
instantiate a new object. I want to get the property value from the
already running object.

Thanks.

</joel>



Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.