all groups > dotnet clr > march 2008 >
You're in the

dotnet clr

group:

Reflection and BaseTypes....


Reflection and BaseTypes.... cwertman@gmail.com
3/1/2008 5:15:55 PM
dotnet clr:
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....

Re: Reflection and BaseTypes.... Peter Duniho
3/1/2008 5:32:11 PM
I'm no expert, but...

[quoted text, click to view]

Type.BaseType property?

Type type =3D ...;

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

Or something like that.

[quoted text, click to view]

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

Re: Reflection and BaseTypes.... cwertman@gmail.com
3/2/2008 6:24:49 AM

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 !=3D typeof(Object))
{
type =3D 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 !=3D typeof(Object))
{
type =3D 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....


On Mar 1, 8:32=A0pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
[quoted text, click to view]
Re: Reflection and BaseTypes.... Peter Duniho
3/2/2008 12:11:12 PM
[quoted text, click to view]

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.

Re: Reflection and BaseTypes.... Mufaka
3/2/2008 1:20:33 PM
[quoted text, click to view]

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");
}
}
Re: Reflection and BaseTypes.... Edgile
3/3/2008 6:05:00 AM

[quoted text, click to view]

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
AddThis Social Bookmark Button