Home All Groups Group Topic Archive Search About

Accessing the managed heap

Author
13 Oct 2005 6:16 AM
MariusI
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?

Author
13 Oct 2005 8:57 PM
Jon Skeet [C# MVP]
MariusI <Mari***@discussions.microsoft.com> wrote:
Show quote
> 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
Author
14 Oct 2005 5:18 AM
MariusI
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
>
Author
14 Oct 2005 5:51 AM
Jon Skeet [C# MVP]
MariusI <Mari***@discussions.microsoft.com> wrote:
> 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 :-)

Well, if you only provide a constructor with an appropriate parameter,
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

AddThis Social Bookmark Button