C# Reflection StackTrack get value
Posted
by John
on Stack Overflow
See other posts from Stack Overflow
or by John
Published on 2010-03-13T13:48:51Z
Indexed on
2010/03/13
13:55 UTC
Read the original article
Hit count: 150
I'm making pretty heavy use of reflection in my current project to greatly simplify communication between my controllers and the wcf services. What I want to do now is to obtain a value from the Session within an object that has no direct access to HttpSessionStateBase (IE: Not a controller). For example, a ViewModel. I could pass it in or pass a reference to it etc. but that is not optimal in my situation.
Since everything comes from a Controller at some point in my scenario I can do the following to walk the sack to the controller where the call originated, pretty simple stuff:
var trace = new System.Diagnostics.StackTrace(); foreach (var frame in trace.GetFrames()) { var type = frame.GetMethod().DeclaringType; var prop = type.GetProperty("Session"); if(prop != null) { // not sure about this part... var value = prop.GetValue(type, null); break; } }
The trouble here is that I can't seem to work out how to get the "instance" of the controller or the Session property so that I can read from it.
© Stack Overflow or respective owner