For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2005 > Getting an object's superclass(es)?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Getting an object's superclass(es)?
intrepid_dw@hotmail.com

2005-01-07, 3:58 am

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

Ciaran

2005-01-10, 3:57 am

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
>



intrepid_dw@hotmail.com

2005-01-10, 3:59 pm

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[color=darkred]
> 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..[color=darkred]
Is[color=darkred]
immediate[color=darkred]
have[color=darkred]
quite...[color=darkred]

Ciaran

2005-01-12, 4:00 pm

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
>



Bruce Wood

2005-01-12, 4:00 pm

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.

intrepid_dw@hotmail.com

2005-01-13, 4:01 pm

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.


Bruce Wood

2005-01-13, 4:01 pm

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?

intrepid_dw@hotmail.com

2005-01-14, 3:59 pm

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?


Bruce Wood

2005-01-15, 3:57 am

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.

Bruce Wood

2005-01-15, 3:57 pm

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?

Bruce Wood

2005-01-17, 8:57 pm

Your method will work, with one minor adjustment. Rather than trying to
create an instance of the base class in order to call yourself again,
you could pass the Type of the object rather than the object itself.
That would make everything easier. In my implementation below, the only
sacrifice is that you can't get all of the fields of the Type class,
but you could eliminate that glitch by giving the methods different
names:

public FieldInfo[] GetAllFields(Object o)
{
return GetAllFields(o.GetType());
}

public FieldInfo[] GetAllFields(Type t)
{
FieldInfo[] typeFields = t.GetFields(/* Specify any binding flags
here */);
Type base = t.BaseType;
if (base != null)
{
FieldInfo[] baseFields = GetAllFields(base);
if (baseFields.Length > 0)
{
FieldInfo[] combinedFields = new
FieldInfo[typeFields.Length + baseFields.Length];
typeFields.CopyTo(combinedFields, 0);
baseFields.CopyTo(combinedFields, typeFields.Length);
typeFields = combinedFields;
}
}
return typeFields;
}

This will return an array of FieldInfo objects, one for each field in
the Type and its base types.

Bruce Wood

2005-01-17, 8:57 pm

Are you sure that the binding flag FlattenHierarchy won't do the trick,
without all of the recursion, etc?

Bruce Wood

2005-01-17, 8:57 pm

I tested my code and it works, with the caveat that "base" is a
reserved word, so you have to change the variable name.

I also tested the FlattenHierarchy flag, and you're right: it doesn't
return all of the fields in the hierarchy. How odd.

intrepid_dw@hotmail.com

2005-01-18, 9:01 pm

Hi again, Bruce! And thanks for your great sample and help. That should
work just fine.

FlattenHierarchy won't do the trick because (at least as the docs say)
it only applies to static members within the hierarchy, so I take that
to mean only those fields declared "static."

Again, thanks so very much for your kind, patient, and generous help!

Thanks,
David


Bruce Wood wrote:
> Are you sure that the binding flag FlattenHierarchy won't do the

trick,
> without all of the recursion, etc?


intrepid_dw@hotmail.com

2005-01-19, 3:59 am

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.


Bruce Wood

2005-01-19, 3:59 am

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?

Bruce Wood

2005-01-19, 3:59 am

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.

Bruce Wood

2005-01-20, 8:57 am

Your method will work, with one minor adjustment. Rather than trying to
create an instance of the base class in order to call yourself again,
you could pass the Type of the object rather than the object itself.
That would make everything easier. In my implementation below, the only
sacrifice is that you can't get all of the fields of the Type class,
but you could eliminate that glitch by giving the methods different
names:

public FieldInfo[] GetAllFields(Object o)
{
return GetAllFields(o.GetType());
}

public FieldInfo[] GetAllFields(Type t)
{
FieldInfo[] typeFields = t.GetFields(/* Specify any binding flags
here */);
Type base = t.BaseType;
if (base != null)
{
FieldInfo[] baseFields = GetAllFields(base);
if (baseFields.Length > 0)
{
FieldInfo[] combinedFields = new
FieldInfo[typeFields.Length + baseFields.Length];
typeFields.CopyTo(combinedFields, 0);
baseFields.CopyTo(combinedFields, typeFields.Length);
typeFields = combinedFields;
}
}
return typeFields;
}

This will return an array of FieldInfo objects, one for each field in
the Type and its base types.

intrepid_dw@hotmail.com

2005-01-20, 8:57 am

Hi again, Bruce! And thanks for your great sample and help. That should
work just fine.

FlattenHierarchy won't do the trick because (at least as the docs say)
it only applies to static members within the hierarchy, so I take that
to mean only those fields declared "static."

Again, thanks so very much for your kind, patient, and generous help!

Thanks,
David


Bruce Wood wrote:
> Are you sure that the binding flag FlattenHierarchy won't do the

trick,
> without all of the recursion, etc?


intrepid_dw@hotmail.com

2005-01-20, 3:59 pm

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?


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com