473,468 Members | 4,540 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Implicit Reference to parent class?

Is there any way in a generic class object to implicitly get a reference to
the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".
Nov 15 '05 #1
5 9289
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to
the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".


Nope, there's nothing like that - good job too, otherwise it would be a
waste of space for the vast majority of cases, which don't require it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
I see your point ! I just wanted to be sure I wasnt missing something ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".


Nope, there's nothing like that - good job too, otherwise it would be a
waste of space for the vast majority of cases, which don't require it.

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

Nov 15 '05 #3
Actually, you can. In your constructor call:

System.Diagnostics.StackFrame frame = new StackFrame(2);
Type callingClass = frame.GetMethod().DeclaringType;

Note that this won't give you the instance that called your constructor, only the class. But that's what you asked for, so hopefully this will work for you. If this is a high-performance application, using Diagnostics may carry a higher cost than you want to pay. But otherwise, this should be fine. BTW, you may need to up the stack frame count (the argument to the StackFrame constructor) if there are more method calls between your constructor and the class that's creating it (e.g., if multiple constructors are called due to subclassing, etc). Hope this helps.

- Ben Blair
{My Name} {at} acm.org

----- JezB wrote: -----

I see your point ! I just wanted to be sure I wasnt missing something ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".
Nope, there's nothing like that - good job too, otherwise it would be a

waste of space for the vast majority of cases, which don't require it.
--

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


Nov 15 '05 #4
Actually, you can. In your constructor call:

System.Diagnostics.StackFrame frame = new StackFrame(2);
Type callingClass = frame.GetMethod().DeclaringType;

Note that this won't give you the instance that called your constructor, only the class. But that's what you asked for, so hopefully this will work for you. If this is a high-performance application, using Diagnostics may carry a higher cost than you want to pay. But otherwise, this should be fine. BTW, you may need to up the stack frame count (the argument to the StackFrame constructor) if there are more method calls between your constructor and the class that's creating it (e.g., if multiple constructors are called due to subclassing, etc). Hope this helps.

- Ben Blair
{My Name} {at} acm.org

----- JezB wrote: -----

I see your point ! I just wanted to be sure I wasnt missing something ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".
Nope, there's nothing like that - good job too, otherwise it would be a

waste of space for the vast majority of cases, which don't require it.
--

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


Nov 15 '05 #5
Ben Blair <an*******@discussions.microsoft.com> wrote:
Actually, you can. In your constructor call:

System.Diagnostics.StackFrame frame = new StackFrame(2);
Type callingClass = frame.GetMethod().DeclaringType;

Note that this won't give you the instance that called your
constructor, only the class. But that's what you asked for,
so hopefully this will work for you.


Except it might not work, because of inlining. All in all, it's a
pretty dodgy thing to rely on, IMO. Far better is to either remove the
need from the design in the first place, or get the caller to pass in a
reference to themselves (or their type, if that's all that's required).

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul | last post by:
Hi all, Here is what I am trying to do. I have a parent class calling a child class. when one function in child class is called, i need to call parent class' function. How can I get parent class'...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
9
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
2
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the...
6
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str;...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
1
by: =?Utf-8?B?R2FsYWhhZA==?= | last post by:
I have an ascx file that inherits an abstract class. Within this abstract class are protected properties. I can access these properties within the page_load event of the parent class, however when...
4
by: Fuzz13 | last post by:
I'm trying to instantiate an object of an inherited class "Chimp" from the parent class "Animal". The Animal class has a constructor that allows for an int legcount, string skintype, string talk, and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.