Reflection and BaseTypes....

C

cwertman

Ok, I am HOPING this is simple and I am missing the quite in front of
my face answer I have not drunk enought caffine to answer.

I have a method that accepts objects of an unknown type and origin.

I am looking to "normailze" their storage in a 5 object database
layer.

2 questions

1)How do I iterate all the base types until I reach System.Object ?

So I have

ClassA
ClassB : ClassA (inherits classa)
ClassC : Class B (inherits classb)


So I pass the mehod an instance of ClassC

How do I get all the classes each derives from ?

I want the list of each item back up the chain so I should get

I have an arbitray class being passed in I have never seen before, so
I cant just call BaseTypes at the desired depth. (Or can I in a loop
somehow ?)

I need ,,,,,,

ClassC
ClassB
ClassA
System.Object

Question 2)

Now, how the heck do I iterate the BaseTypes and get the value that
exists witin ONLY the base the

So

ClassB has a property of FirstName and ClassC inherits ClassB

When I am iterating them I need only the Properties that exist in
ClassB (Not ClassC) and Not ClassA that ClassB inherits from)

Many thanks my brain is feeling like scramled eggs.

I wrote and ObjectDataBase layer for SQL2005 but I wrote it 2 years
ago and in VB I am wanting to convert it now to C# and tweak some
things like normailizing the storage of Base types....

http://www.objectgoo.com
 
P

Peter Duniho

I'm no expert, but...

1)How do I iterate all the base types until I reach System.Object ?

Type.BaseType property?

Type type = ...;

while (type != typeof(Object))
{
type = type.BaseType;
}

Or something like that.
[...]
Question 2)

Now, how the heck do I iterate the BaseTypes and get the value that
exists witin ONLY the base the

Type.GetProperties(BindingFlags.DeclaredOnly), I think?

Pete
 
C

cwertman

Excellent ,

Thanks a ton.....

#1 works like a charm, #2 I cant seem to get to work...? But I see I
must be doing something wrong because it makes sense...

while (type != typeof(Object))
{
type = type.BaseType;
Response.Write(type.ToString() + " : ");

foreach (PropertyInfo item in type.GetProperties())
{
Response.Write(item.Name + "PROP<br>");
}

}

Works but returns all properties down the chain with the items in the
prior assy

while (type != typeof(Object))
{
type = type.BaseType;
Response.Write(type.ToString() + " : ");

foreach (PropertyInfo item in
type.GetProperties(BindingFlags.DeclaredOnly))
{
Response.Write(item.Name + "PROP<br>");
}

}


Dosent return anything :(

Ive trie composites like....
BindingFlags.DeclaredOnly | Public....etc

Anything other than the default constructor shows me nothing ?
wierd....


I'm no expert, but...

1)How do I iterate all the base types until I reach System.Object ?

Type.BaseType property?

     Type type = ...;

     while (type != typeof(Object))
     {
         type = type.BaseType;
     }

Or something like that.
[...]
Question 2)
Now, how the heck do I iterate the BaseTypes and get the value that
exists witin ONLY the base the

Type.GetProperties(BindingFlags.DeclaredOnly), I think?

Pete
 
P

Peter Duniho

[...]
#1 works like a charm, #2 I cant seem to get to work...? But I see I
must be doing something wrong because it makes sense...

Well, I think it does too. :)

I don't see anything obviously wrong with how you wrote the loop. So I
can only assume that I am (and you are too) missing some crucial piece of
information about how you are supposed to use that particular overload. I
know so little about reflection, I wouldn't even hazard a guess, though I
suppose I might play around with it later and see if I can come up with
anything.

In the meantime, hopefully one of the folks here who use reflection a
little more regularly can offer some advice.

Pete
 
M

Mufaka

Excellent ,

Thanks a ton.....

#1 works like a charm, #2 I cant seem to get to work...? But I see I
must be doing something wrong because it makes sense...

while (type != typeof(Object))
{
type = type.BaseType;
Response.Write(type.ToString() + " : ");

foreach (PropertyInfo item in type.GetProperties())
{
Response.Write(item.Name + "PROP<br>");
}

}

Works but returns all properties down the chain with the items in the
prior assy

while (type != typeof(Object))
{
type = type.BaseType;
Response.Write(type.ToString() + " : ");

foreach (PropertyInfo item in
type.GetProperties(BindingFlags.DeclaredOnly))
{
Response.Write(item.Name + "PROP<br>");
}

}


Dosent return anything :(

Ive trie composites like....
BindingFlags.DeclaredOnly | Public....etc

Anything other than the default constructor shows me nothing ?
wierd....


I'm no expert, but...

1)How do I iterate all the base types until I reach System.Object ?
Type.BaseType property?

Type type = ...;

while (type != typeof(Object))
{
type = type.BaseType;
}

Or something like that.
[...]
Question 2)
Now, how the heck do I iterate the BaseTypes and get the value that
exists witin ONLY the base the
Type.GetProperties(BindingFlags.DeclaredOnly), I think?

Pete

I'm certainly no reflection expert either, but it looks like you can
check the PropertyInfo.DeclaringType for a match against your objects
BaseType.

private void PrintBaseTypeProperties(Type type)
{
foreach (PropertyInfo item in type.GetProperties())
{
if (item.DeclaringType == type.BaseType)
{
richTextBox1.AppendText(item.Name + "\r\n");
}
}
}
 
E

Edgile

Type.GetProperties(BindingFlags.DeclaredOnly), I think?

Correct try, but not enough.
Type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance |
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);

If you are not interested in static or non-public properties, leave those
out from the OR statement.
 

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

Top