Determining base class

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

Is there some way to determine what base class an instance of a object was
derived from?
 
Chuck

get the type of the object of interest, then use the BaseType property.
Consider something like this:

static void PrintHeirarchy(object o){
Type t = o.GetType();
string curName = t.FullName;
string baseName = t.BaseType.FullName;
Console.WriteLine("Cur Type: {0}\nBase Type: {1}",curName,baseName);
}

regards
roy fine
 
and if you want to view the entire heirarchy - try something like this:

static void PrintHeirarchy(object o){
int indent = 0;
string padds = "";
Type t = o.GetType();
while(true){
string p = padds.PadRight(indent*2,' ');
Console.WriteLine("{0}Class: {1}",p,t.FullName);
t = t.BaseType;
if(t == null) break;
indent++;
}
}
regards
roy fine
 
Excellent, thanks Roy.

Roy Fine said:
Chuck

get the type of the object of interest, then use the BaseType property.
Consider something like this:

static void PrintHeirarchy(object o){
Type t = o.GetType();
string curName = t.FullName;
string baseName = t.BaseType.FullName;
Console.WriteLine("Cur Type: {0}\nBase Type: {1}",curName,baseName);
}

regards
roy fine
 
Rather, you should do this:

// Get the type.
Type t = o.GetType();

do
{
string p = padds.PadRight(indent*2,' ');
Console.WriteLine("{0}Class: {1}",p,t.FullName);
indent++;
t = t.BaseType;
} while (t != null)

Using just a value of true in a loop construct is pretty bad practice.
 
Nicholas,

the use of true in a loop construct is perfectly valid and your position of
"bad practice" is purely one of personal preference - i respect that, but it
is in the end, it is but your preference (many may agree with you - but
still, it is a personal preference).

consider this technique:

static void PrintHeirarchy(object o){
int indent = 0;
string padds = "";
for(Type tp = o.GetType(); tp != null; tp = tp.BaseType){
string p = padds.PadRight(indent*2,' ');
Console.WriteLine("{0}Class: {1}",p,tp.FullName);
indent++;
}
Console.WriteLine("\n\n");

that is a better yet implementation - but then better by whose standards.
in the end, this code snip was not about style, as you are trying to coherce
the thread in that direction, rather it is about access to meta data.

best regards
roy fine

Nicholas Paldino said:
Rather, you should do this:

// Get the type.
Type t = o.GetType();

do
{
string p = padds.PadRight(indent*2,' ');
Console.WriteLine("{0}Class: {1}",p,t.FullName);
indent++;
t = t.BaseType;
} while (t != null)

Using just a value of true in a loop construct is pretty bad practice.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Roy Fine said:
and if you want to view the entire heirarchy - try something like this:

static void PrintHeirarchy(object o){
int indent = 0;
string padds = "";
Type t = o.GetType();
while(true){
string p = padds.PadRight(indent*2,' ');
Console.WriteLine("{0}Class: {1}",p,t.FullName);
t = t.BaseType;
if(t == null) break;
indent++;
}
}
regards
roy fine

object
was
 
Well, since we're trying to drag every last bit of improvement out of
this.....

--Why are we needlessly dragging multiplication into this?
--Why are we incrementing indent so far from where we are using it?
--Why are we doing something bizarre like padding an empty string when
there's a perfectly good ctor to do what we want?

// Get the type.
Type t = o.GetType();
do
{
string p = new String(' ', indent);
indent +=2;
Console.WriteLine("{0}Class: {1}",p,t.FullName);
t = t.BaseType;
} while (t != null)


I'm also torn as to which of the following, in the long run, would be
better:

1. As above

2.
Console.Write(new String(' ', indent);
Console.WriteLine("Class: {1}",t.FullName);

3.

Console.Write(new String(' ', indent);
Console.Write("Class: ");
Console.WriteLine(t.FullName);

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
James Curran said:
Well, since we're trying to drag every last bit of improvement out of
this.....

--Why are we needlessly dragging multiplication into this?
--Why are we incrementing indent so far from where we are using it?
--Why are we doing something bizarre like padding an empty string when
there's a perfectly good ctor to do what we want?

you're right of course.... how could i have been so incompetent as to have
made such a sophomoric blunder. with this improved version, is a lot easier
to realize the full meaning of what the OP was really looking for!


static void PrintHeirarchy(object o){
int indent = 0;
for(Type tp = o.GetType(); tp != null; tp = tp.BaseType){
Console.WriteLine("{0}Class: {1}","".PadRight((indent++)*2,'
'),tp.FullName);
}

regards
roy fine
 

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

Back
Top