468,771 Members | 1,923 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Which class is calling?

I have a base class with a method that is to be called in the constructor of
the inheritting classes. Is there any way of determining, say, the Type of
the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of the
three constructors, but I need to know which constructor was calling it (so
in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg
Nov 15 '05 #1
8 1346
Hi Greg,

You would have to examine the call stack which is expensive and unreliable
IMO.
You might consider another way to solve your problem.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of
the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of the three constructors, but I need to know which constructor was calling it (so in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg

Nov 15 '05 #2
You really need to change your design. Why can't each constructor do its own
init as appropriate? if all classes have a block of init code in common,
this can go in a protected method in the base class.

Just in case someone suggests it, calling virtual methods from a constructor
is a really bad idea, since the virtual method calls happen before any other
initialisation that the constructor does. This can produce unexpected
results.

Hope this helps

Regards

Ron

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of
the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of the three constructors, but I need to know which constructor was calling it (so in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg

Nov 15 '05 #3
Miha,

I am curious, can you show examples of where the call stack is
unreliable? I'll definitely agree that it is expensive, but I haven't heard
claims that it is unreliable (and security would be compromised as a result,
because stack walks are done in order to handle a lot of the security in
..NET).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Greg,

You would have to examine the call stack which is expensive and unreliable
IMO.
You might consider another way to solve your problem.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor
of
the inheritting classes. Is there any way of determining, say, the Type

of the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of

the
three constructors, but I need to know which constructor was calling it

(so
in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg


Nov 15 '05 #4
Hi Nicholas,

I am not sure on reliablity, AFAIK compiler could optimize some calls, do
things inline - stuff like that.
Even if it is not doing this already it can in future.
I didn't ever test it though.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...
Miha,

I am curious, can you show examples of where the call stack is
unreliable? I'll definitely agree that it is expensive, but I haven't heard claims that it is unreliable (and security would be compromised as a result, because stack walks are done in order to handle a lot of the security in
.NET).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Greg,

You would have to examine the call stack which is expensive and unreliable
IMO.
You might consider another way to solve your problem.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor
of
the inheritting classes. Is there any way of determining, say, the
Type of the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each

of the
three constructors, but I need to know which constructor was calling
it (so
in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg



Nov 15 '05 #5
<"Miha Markic" <miha at rthand com>> wrote:
I am not sure on reliablity, AFAIK compiler could optimize some calls, do
things inline - stuff like that.
Even if it is not doing this already it can in future.
I didn't ever test it though.


The JIT inlining functions can definitely have a nasty effect on a
stack trace. (I was suspicious of this until I actually saw it.) Here's
a program which demonstrates it:

using System;
using System.Diagnostics;

class Test
{
static StackTrace GetTrace()
{
return new System.Diagnostics.StackTrace();
}

static StackTrace Intermediate()
{
return GetTrace();
}

static void Main(string[] args)
{
Console.WriteLine (Intermediate());
}
}

Compiled with debug:
at Test.GetTrace()
at Test.Intermediate()
at Test.Main(String[])

Compiled without debug information:
at Test.Main(String[])

(Adding [MethodImpl(MethodImplOptions.NoInlining)] to Intermediate and
GetTrace makes it get it right.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
greg,

it does sound like your design could be organized in a better way. i'm not
exactly sure what behavior you're going for, but it seems like you want
this:

when C is constructed, the C object calls the B constructor, which calls
the A constructor, which adds stuff for A, then the B constructor adds
stuff for B, then the C constructor adds stuff for C.

if AddStuff works in different ways depending on which class it is called
from, eg it does action 1 when A calls it, action 2 when B calls it, and
action 3 when C calls it, then AddStuff should really be a different method
in each of these classes. i suggest looking into the virtual and new
keywords for this. this is a common problem in object oriented development
which is solved by Polymorphism. i've posted one example below - notice i
defined A's AddStuff with the virtual keyword and the others are defined
with the new keyword in the method signature. when you run this code, the
output is A->B->C.

let me know if you have any other questions.

jeff.

namespace ConsoleApplication4
{
class Class1
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}

class A
{
public A()
{
this.AddStuff();
}

public virtual void AddStuff()
{
Console.WriteLine("AddStuff - A");
}
}
class B : A
{
public B()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - B");
}
}
class C : B {
public C()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - C");
}
}
}

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 15 '05 #7
*goes crosseyed*
"Jeff Callahan" <Je*****@online.microsoft.com> wrote in message
news:f6**************@cpmsftngxa07.phx.gbl...
greg,

it does sound like your design could be organized in a better way. i'm not exactly sure what behavior you're going for, but it seems like you want
this:

when C is constructed, the C object calls the B constructor, which calls
the A constructor, which adds stuff for A, then the B constructor adds
stuff for B, then the C constructor adds stuff for C.

if AddStuff works in different ways depending on which class it is called
from, eg it does action 1 when A calls it, action 2 when B calls it, and
action 3 when C calls it, then AddStuff should really be a different method in each of these classes. i suggest looking into the virtual and new
keywords for this. this is a common problem in object oriented development which is solved by Polymorphism. i've posted one example below - notice i
defined A's AddStuff with the virtual keyword and the others are defined
with the new keyword in the method signature. when you run this code, the
output is A->B->C.

let me know if you have any other questions.

jeff.

namespace ConsoleApplication4
{
class Class1
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}

class A
{
public A()
{
this.AddStuff();
}

public virtual void AddStuff()
{
Console.WriteLine("AddStuff - A");
}
}
class B : A
{
public B()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - B");
}
}
class C : B {
public C()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - C");
}
}
}

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 15 '05 #8
Hi Greg

I posted this last night, but it didn't turn up on the NG - not an unknown
event methinks.

You really need to change your design. Why can't each constructor do its own
init as appropriate? if all classes have a block of init code in common,
this can go in a protected method in the base class.

Just in case someone suggests it, calling virtual methods from a constructor
is a really bad idea, since the virtual method calls happen before any other
initialisation that the constructor does. This can produce unexpected
results.

Hope this helps

Regards

Ron
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Buddy Ackerman | last post: by
10 posts views Thread by jim.brown | last post: by
8 posts views Thread by Jeffrey Barish | last post: by
2 posts views Thread by Steven D'Aprano | last post: by
7 posts views Thread by Peter Bradley | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.