|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Accessing the managed heapconstructor. I'm doing the following in the class constructor: public Constructor() { StackTrace st = new StackTrace(); bool firstMethodOutsideOfCtor = false; for (int i = 0; i < st.FrameCount && !firstMethodOutsideOfCtor; i++) { StackFrame frame = st.GetFrame(i); MethodBase mBase = frame.GetMethod(); if ((firstMethodOutsideOfCtor = !mBase.IsConstructor)) { Type handle = mBase.DeclaringType; } } So as you see, i can get the type of the invoking class, but i need a reference. Is there a solution to this problem? MariusI <Mari***@discussions.microsoft.com> wrote:
Show quote > Hi i want to obtain a reference to the object which invoked the class Yes - pass it into the constructor. There's no other solution, and > constructor. I'm doing the following in the class constructor: > > public Constructor() > { > StackTrace st = new StackTrace(); > > bool firstMethodOutsideOfCtor = false; > for (int i = 0; i < st.FrameCount && !firstMethodOutsideOfCtor; > i++) > { > StackFrame frame = st.GetFrame(i); > MethodBase mBase = frame.GetMethod(); > > if ((firstMethodOutsideOfCtor = !mBase.IsConstructor)) > { > Type handle = mBase.DeclaringType; > } > } > > So as you see, i can get the type of the invoking class, but i need a > reference. Is there a solution to this problem? there might not even *be* another object (it might be a static method which creates the instance). -- 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 I could of course do that, but i was hoping for a more sleek solution. The
classes which use the below code are form classes, and the parent reference is used to obtain which module the form is currently running under. The module is then used to obtain security parameters which controls how the form displays. The form access control is done through a base class, so my form classes just derive from the secure base class and voila GUI security is enabled. If i need to pass a reference, each programmer which creates new forms has to remember to do this in order to enable security. I was hoping for the more sleek solution :-) Show quote "Jon Skeet [C# MVP]" wrote: > MariusI <Mari***@discussions.microsoft.com> wrote: > > Hi i want to obtain a reference to the object which invoked the class > > constructor. I'm doing the following in the class constructor: > > > > public Constructor() > > { > > StackTrace st = new StackTrace(); > > > > bool firstMethodOutsideOfCtor = false; > > for (int i = 0; i < st.FrameCount && !firstMethodOutsideOfCtor; > > i++) > > { > > StackFrame frame = st.GetFrame(i); > > MethodBase mBase = frame.GetMethod(); > > > > if ((firstMethodOutsideOfCtor = !mBase.IsConstructor)) > > { > > Type handle = mBase.DeclaringType; > > } > > } > > > > So as you see, i can get the type of the invoking class, but i need a > > reference. Is there a solution to this problem? > > Yes - pass it into the constructor. There's no other solution, and > there might not even *be* another object (it might be a static method > which creates the instance). > > -- > 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 > MariusI <Mari***@discussions.microsoft.com> wrote:
> I could of course do that, but i was hoping for a more sleek solution. The Well, if you only provide a constructor with an appropriate parameter, > classes which use the below code are form classes, and the parent reference > is used to obtain which module the form is currently running under. The > module is then used to obtain security parameters which controls how the form > displays. The form access control is done through a base class, so my form > classes just derive from the secure base class and voila GUI security is > enabled. If i need to pass a reference, each programmer which creates new > forms has to remember to do this in order to enable security. I was hoping > for the more sleek solution :-) then it's not a case of "remembering" to do it - the compiler will complain if they don't. -- 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 |
|||||||||||||||||||||||