Getting reference to running object

  • Thread starter Thread starter Joel
  • Start date Start date
J

Joel

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>
 
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,

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("IWip");
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 said:
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]
- (e-mail address removed)

Joel said:
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>
 
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]
- (e-mail address removed)

Joel said:
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("IWip");
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 said:
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]
- (e-mail address removed)

Joel said:
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>
 
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 said:
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]
- (e-mail address removed)

Joel said:
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("IWip");
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 said:
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]
- (e-mail address removed)

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>
 
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]
- (e-mail address removed)

Joel said:
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 said:
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]
- (e-mail address removed)

Joel said:
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("IWip");
if(t!=null)
{
PropertyInfo pi=t.GetProperty("IsWip");
}
}

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

</joel>

in message 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]
- (e-mail address removed)

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>
 
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 said:
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("IWip");
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 said:
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]
- (e-mail address removed)

Joel said:
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>
 
Back
Top