Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Getting an object's superclass(es)?
Hi, all. Possibly dumb question here, but I'm going to ask it anyway..

Let's say I have a class hierarchy as follows:

Class a

Class b:a

Class c:b

Class d:c

For some arbitrary and perverse reason, let's say I had a desire to
traverse that class hierarchy starting from an instance of Class d. Is
it possible in C# to get d's immediate superclass, then that immediate
superclass, and so on.

Is this even possible? I've looked through the Type methods and have
seen a few things that look like it might come close, but not quite...
Thanks,
David


Report this thread to moderator Post Follow-up to this message
Old Post
intrepid_dw@hotmail.com
01-07-05 08:58 AM


Re: Getting an object's superclass(es)?
Use typeof(class) or object.GetType() to get the type of the object.
The "immediate superclass" is referred to be C# as the direct base type.
accesible on the type by usign the BaseType property. This will give you
another Type object which you can just get the FullName of.

Hope this helps

Ciaran


<intrepid_dw@hotmail.com> wrote in message
news:1105063089.324013.268350@z14g2000cwz.googlegroups.com...
> Hi, all. Possibly dumb question here, but I'm going to ask it anyway..
>
> Let's say I have a class hierarchy as follows:
>
> Class a
>
> Class b:a
>
> Class c:b
>
> Class d:c
>
> For some arbitrary and perverse reason, let's say I had a desire to
> traverse that class hierarchy starting from an instance of Class d. Is
> it possible in C# to get d's immediate superclass, then that immediate
> superclass, and so on.
>
> Is this even possible? I've looked through the Type methods and have
> seen a few things that look like it might come close, but not quite...
> Thanks,
> David
>



Report this thread to moderator Post Follow-up to this message
Old Post
Ciaran
01-10-05 08:57 AM


Re: Getting an object's superclass(es)?
Ciaran:

Thanks so much for taking the time to offer your help.

Let me extend a bit on what I was trying to accomplish, which I didn't
do very well in the original post.

If I know that I can get the "superior" object's type with GetType,
what I'd like to do is grab a reference to that superclass *instance*
within the subclassed object, eg (from the example below, given the
a-b-c-d hierarchy)

say something like:

d ClassDInst = new d();
c ClassCInst = (c)d;

Now, given that I want to make this generic, I'm looking for what I
guess would have to be termed a "runtime" way of casting an object, as
if I could perform a cast just with a Type object. Here's what I'd like
to do "conceptually" (I know this won't work syntactically, but it
serves for illustration)

Type someType = d.GetType().BaseType // get superclass type
c ClassCInst = (someType) d;  //I know, in this case, 'someType' is
'c',

All in all, what I'm wanting to do is to develop generic
reflection-type code to iterate through the superclassed objects in a
class hierarchy of arbitrary depth, starting at the lowest subclass and
eventually working my way to the top 'Object' class.

Thanks again for taking the time to help.

-David









Ciaran wrote:
> Use typeof(class) or object.GetType() to get the type of the object.
> The "immediate superclass" is referred to be C# as the direct base
type.
> accesible on the type by usign the BaseType property. This will give
you
> another Type object which you can just get the FullName of.
>
> Hope this helps
>
> Ciaran
>
>
> <intrepid_dw@hotmail.com> wrote in message
> news:1105063089.324013.268350@z14g2000cwz.googlegroups.com... 
anyway.. 
Is 
immediate 
have 
quite... 


Report this thread to moderator Post Follow-up to this message
Old Post
intrepid_dw@hotmail.com
01-10-05 08:59 PM


Re: Getting an object's superclass(es)?
Use typeof(class) or object.GetType() to get the type of the object.
The "immediate superclass" is referred to be C# as the direct base type.
accesible on the type by usign the BaseType property. This will give you
another Type object which you can just get the FullName of.

Hope this helps

Ciaran


<intrepid_dw@hotmail.com> wrote in message
news:1105063089.324013.268350@z14g2000cwz.googlegroups.com...
> Hi, all. Possibly dumb question here, but I'm going to ask it anyway..
>
> Let's say I have a class hierarchy as follows:
>
> Class a
>
> Class b:a
>
> Class c:b
>
> Class d:c
>
> For some arbitrary and perverse reason, let's say I had a desire to
> traverse that class hierarchy starting from an instance of Class d. Is
> it possible in C# to get d's immediate superclass, then that immediate
> superclass, and so on.
>
> Is this even possible? I've looked through the Type methods and have
> seen a few things that look like it might come close, but not quite...
> Thanks,
> David
>



Report this thread to moderator Post Follow-up to this message
Old Post
Ciaran
01-12-05 09:00 PM


Re: Getting an object's superclass(es)?
What operations are you hoping to perform on the base class(es) once
you determine what they are?

Iterating through the class hierarchy in the way that you have
described is not possible, but perhaps if we understand better what
you're hoping to achieve in the end, we can attack your problem from a
different angle.


Report this thread to moderator Post Follow-up to this message
Old Post
Bruce Wood
01-12-05 09:00 PM


Re: Getting an object's superclass(es)?
Hi, Bruce

Thanks for your help and questions.

To be honest, my end goal is merely to have the ability to gain a
runtime reference to the immediate supertype of an *arbitrary* subtype.
I know I can always get a reference through explicit casting to the
supertype, but in the generic sense I don't know what the "hard"
supertype will be.

I suspected what I wanted to do was not possible, because no matter
what path you traverse, something ends up requiring a cast from
(Object), and that requires the specification of a hard type at compile
time, which is precisely what I *don't* know in the general case.

Does that help any?

As I suspected, it is probably not possible, but I wanted to ask more
learned people their opinion on the notion.

Thanks,
David


Bruce Wood wrote:
> What operations are you hoping to perform on the base class(es) once
> you determine what they are?
>
> Iterating through the class hierarchy in the way that you have
> described is not possible, but perhaps if we understand better what
> you're hoping to achieve in the end, we can attack your problem from
a
> different angle.


Report this thread to moderator Post Follow-up to this message
Old Post
intrepid_dw@hotmail.com
01-13-05 09:01 PM


Re: Getting an object's superclass(es)?
What I want to know, though, is once you have a reference to the
supertype, what are you going to do with that reference? Call its
methods? Serialize it? Print out its name?

I ask because you're asking for instructions for implementing a
solution, but I don't know what the problem is. If I better understood
the larger problem, perhaps I could suggest a different solution. For
example, if what you want to do is print out the names of the methods
and properties at each level of the hierarchy, yes you can do that
using reflection. On the other hand, if you want to upcast and then
pass the base object around then there's no way to do that
"generically," but then it's unclear what it would even mean to do this
"generically."

What is the larger problem you're trying to solve by walking the
hierarchy generically?


Report this thread to moderator Post Follow-up to this message
Old Post
Bruce Wood
01-13-05 09:01 PM


Re: Getting an object's superclass(es)?
Hi, Bruce

I want to perform the upcast and pass that base object to other
methods, and its pretty clear that's not possible.

In the particular, I wanted to interrogate an object and get its fields
via the GetFields() method, but it returns only the fields declared on
the "current" subtype - not any of its inherited fields. I found no
binding flags that would accomplish that, either, so I began to
theorize that I would have to interrogate each superclass until I
reached the fundamental Object type. A recursive method came to mind,
which then led to the need to pass the object types, eg

(please excuse this very crude pseudo code, its intended to show a
concept)

fieldList = x.getAllFields(myObject)

and

fieldListType getAllFields(object someObject)
{
static fieldListType fieldList;

if (object.GetType()=='System.Type')
return fieldList;
else
{
fieldList.AddFields(Type.GetFields(someObject));
fieldList.AddFields(getAllFields(someObject.superclass) //the
superclass ref. here is not literal, just to express the idea of
passing a ref to the superclass)
}
}

It's pretty clear that won't work quite as I had envisioned, so I'll
have to try a different approach. And there's quite probably a much
simpler (and infinitely more elegant) solution out there...making
things too complicated is one thing I do best :)

Thanks for all the help/comments,
David


Bruce Wood wrote:
> What I want to know, though, is once you have a reference to the
> supertype, what are you going to do with that reference? Call its
> methods? Serialize it? Print out its name?
>
> I ask because you're asking for instructions for implementing a
> solution, but I don't know what the problem is. If I better
understood
> the larger problem, perhaps I could suggest a different solution. For
> example, if what you want to do is print out the names of the methods
> and properties at each level of the hierarchy, yes you can do that
> using reflection. On the other hand, if you want to upcast and then
> pass the base object around then there's no way to do that
> "generically," but then it's unclear what it would even mean to do
this
> "generically."
>
> What is the larger problem you're trying to solve by walking the
> hierarchy generically?


Report this thread to moderator Post Follow-up to this message
Old Post
intrepid_dw@hotmail.com
01-14-05 08:59 PM


Re: Getting an object's superclass(es)?
What operations are you hoping to perform on the base class(es) once
you determine what they are?

Iterating through the class hierarchy in the way that you have
described is not possible, but perhaps if we understand better what
you're hoping to achieve in the end, we can attack your problem from a
different angle.


Report this thread to moderator Post Follow-up to this message
Old Post
Bruce Wood
01-15-05 08:57 AM


Re: Getting an object's superclass(es)?
What I want to know, though, is once you have a reference to the
supertype, what are you going to do with that reference? Call its
methods? Serialize it? Print out its name?

I ask because you're asking for instructions for implementing a
solution, but I don't know what the problem is. If I better understood
the larger problem, perhaps I could suggest a different solution. For
example, if what you want to do is print out the names of the methods
and properties at each level of the hierarchy, yes you can do that
using reflection. On the other hand, if you want to upcast and then
pass the base object around then there's no way to do that
"generically," but then it's unclear what it would even mean to do this
"generically."

What is the larger problem you're trying to solve by walking the
hierarchy generically?


Report this thread to moderator Post Follow-up to this message
Old Post
Bruce Wood
01-15-05 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:42 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.