Accessing the managed heap

G

Guest

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?
 
J

Jon Skeet [C# MVP]

MariusI said:
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).
 
G

Guest

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 :)
 
J

Jon Skeet [C# MVP]

MariusI said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top