A: 

I am not sure that I fully understand what you want, but if you want to know the type in which the method for a certain stack frame is declared, I think this code returns that:

StackTrace trace = new StackTrace();    
Type methodOwner = trace.GetFrame(0).GetMethod().DeclaringType;

You will of course need to pass the index for the frame that you are interested in (I use 0 as example).

Fredrik Mörk
I think he means the instance, not the type.
siz
Yes, 'object' means instance of a type ;)
Cecil Has a Name
Yes, I realized that (it's actually rather clear in your question; I was a bit tired I guess...)
Fredrik Mörk
+2  A: 

I'm pretty sure that this is not possible. Here's why:

  1. This could break type safety, since anyone can lookup a frame, get the object regardless of which AppDomain\Thread they are executing on or permission they have.
  2. The 'this' (C#) identifier is really just an argument to the instance method (the first), so in reality their is no difference between static methods and instance methods, the compiler does its magic to pass the right this to an instance method, which of course means that you will need to have access to all method arguments to get the this object. (which StackFrame does not support)

It might be possible by using unsafe code to get the pointer of the first argument to an instance method and then casting it to the right type, but I have no knowledge of how to do that, just an idea.

BTW you can imagine instance methods after being compiled to be like C# 3.0 extension methods, they get the this pointer as their first argument.

Y Low
I know that the this pointer is passed to the called method on the call stack by the caller, which is why the nonexistent GetObject() would return null if it was a static method, and a copy of the canonical 'this' pointer in an instance method.However, I agree with your #1 argument, except that the access to the object could easily be restricted by requiring the appropriate permission by design.
Cecil Has a Name
The real problem is that a StackFrame is just some metadata about your code, it's not a live version of your code.
Y Low
As I feared. But I hoped there was some other classes to avail.
Cecil Has a Name