473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Walking inheritance hierarchy - why doesn'y this work?

Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

' .... reflection to set field values at this inheritance tier ...

'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.

Is there another way to walk the hierarchy?

Thx

S
Jun 27 '08 #1
11 1373
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

' .... reflection to set field values at this inheritance tier ...

'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.

Is there another way to walk the hierarchy?

Thx

S
Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)

--
Tom Shelton
Jun 27 '08 #2
Tom Shelton wrote:
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
>Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

' .... reflection to set field values at this inheritance tier ...

'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.

Is there another way to walk the hierarchy?

Thx

S

Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)
Thx Tom

"Option Strict On disallows implicit conversions from object to T"

(I read elsewhere that there was a problem upcasting generics with
earlier versions of c#. Presumably all the IoCs get round this though)


Jun 27 '08 #3
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
Tom Shelton wrote:
>On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
>>Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

' .... reflection to set field values at this inheritance tier ...

'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.

Is there another way to walk the hierarchy?

Thx

S

Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)

Thx Tom

"Option Strict On disallows implicit conversions from object to T"

(I read elsewhere that there was a problem upcasting generics with
earlier versions of c#. Presumably all the IoCs get round this though)
First, let me apologize... I didn't really understand what you were trying to
accomplish - and the fact is, you can't get there from here - at least not the
way your trying to do it. In other, words you can't dynamically specify the
type of a casst at runtime - and Convert.ChangeType won't help you there...
It basically does stuff like changing strings to numbers, etc - basically it
deals with types that implement IConvertable.

That said, you can definately walk the tree... But your going to have to do
it something like:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Reflection

Module Module1

Sub Main()
Dim cc As New C()
WalkInheritance(cc)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T)
Console.WriteLine(o.GetType().FullName)
WalkInheritance(o, o.GetType().BaseType)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T, ByVal bType
As Type)
If Not bType Is Nothing Then
Console.WriteLine(bType.FullName)
bType = bType.BaseType
WalkInheritance(o, bType)

End If
End Sub

Private MustInherit Class BaseClass

End Class

Private Class A
Inherits BaseClass
End Class

Private Class B
Inherits A
End Class

Private Class C
Inherits B
End Class
End Module

I hope this helps you some...

--
Tom Shelton
Jun 27 '08 #4
Tom,

Are you sure that you are not helping somebody doing his schoolwork?

I see not any practical sense in the questions Simon sends, but it can be
schoolwork.

Cor

"Tom Shelton" <to*********@comcastXXXXXXX.netschreef in bericht
news:do******************************@comcast.com. ..
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
>Tom Shelton wrote:
>>On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance
hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

' .... reflection to set field values at this inheritance tier ...

'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType),
_
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.

Is there another way to walk the hierarchy?

Thx

S

Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)

Thx Tom

"Option Strict On disallows implicit conversions from object to T"

(I read elsewhere that there was a problem upcasting generics with
earlier versions of c#. Presumably all the IoCs get round this though)

First, let me apologize... I didn't really understand what you were
trying to
accomplish - and the fact is, you can't get there from here - at least not
the
way your trying to do it. In other, words you can't dynamically specify
the
type of a casst at runtime - and Convert.ChangeType won't help you
there...
It basically does stuff like changing strings to numbers, etc - basically
it
deals with types that implement IConvertable.

That said, you can definately walk the tree... But your going to have to
do
it something like:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Reflection

Module Module1

Sub Main()
Dim cc As New C()
WalkInheritance(cc)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T)
Console.WriteLine(o.GetType().FullName)
WalkInheritance(o, o.GetType().BaseType)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T, ByVal
bType
As Type)
If Not bType Is Nothing Then
Console.WriteLine(bType.FullName)
bType = bType.BaseType
WalkInheritance(o, bType)

End If
End Sub

Private MustInherit Class BaseClass

End Class

Private Class A
Inherits BaseClass
End Class

Private Class B
Inherits A
End Class

Private Class C
Inherits B
End Class
End Module

I hope this helps you some...

--
Tom Shelton
Jun 27 '08 #5
On 2008-06-21, Cor Ligthert[MVP] <no************@planet.nlwrote:
Tom,

Are you sure that you are not helping somebody doing his schoolwork?
No, I'm not sure really...

--
Tom Shelton
Jun 27 '08 #6
Cor Ligthert[MVP] wrote:
Tom,

Are you sure that you are not helping somebody doing his schoolwork?

I see not any practical sense in the questions Simon sends, but it can
be schoolwork.

Cor
In my defense (and perhaps I'm being too defensive)...

1) I'm rather too old for school - I grew up with the Punk Rock.

2) From a practical perspective, as I suggested earlier (and perhaps I'm
wrong), I would have thought that any dependency injection framework
would have come up against this issue - so I'm not sure why you would
think it is 'homework-ish'. I've only perused some of them briefly, but
Ninject certainly does field injection (via tagging in the class
itself), so whether reflection is being used or some other way of going
in under the hood, this issue would surely have come up.

Funnily enough my question reflects a 'real-world' problem. The product
I work on holds a string of data in it's db which is populated into a
class which itself inherits from other classes which inherit ... and so
on. ATM we effectively pass the data into the base class which then
takes what it needs from the data to populate itself, calls a
mustoverrides method so any inheriting class can then take what it needs
from the data and pass it on. Effectively the class is populating
itself. I was simply looking for an alternative way of populating the
class using a factory to inject the data into the fields. The problem I
encountered was that the fields exposed via Reflection's GetFields are
only from one particular inheritance tier. From reading other threads,
the suggestion was to walk the inheritence hierarchy to get at the
fields from different tiers.

Perhaps I should have asked how you get at the fields from the different
inheritance tiers using reflection and maybe the answer would have been
different.

3) Wrt my previous question re Lambda expressions, yes I'm trying to
learn as it's all pretty new to me. I find these groups really helpful
for my understanding and I'm likely to post other rather abstract
questions about how to get on in FP. I guess that's just my learning style.

Am I breaching use-net-tiquette?

Jun 27 '08 #7
Tom Shelton wrote:
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
>Tom Shelton wrote:
>>On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

' .... reflection to set field values at this inheritance tier ...

'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.

Is there another way to walk the hierarchy?

Thx

S
Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)
Thx Tom

"Option Strict On disallows implicit conversions from object to T"

(I read elsewhere that there was a problem upcasting generics with
earlier versions of c#. Presumably all the IoCs get round this though)

First, let me apologize... I didn't really understand what you were trying to
accomplish - and the fact is, you can't get there from here - at least not the
way your trying to do it. In other, words you can't dynamically specify the
type of a casst at runtime - and Convert.ChangeType won't help you there...
It basically does stuff like changing strings to numbers, etc - basically it
deals with types that implement IConvertable.

That said, you can definately walk the tree... But your going to have to do
it something like:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Reflection

Module Module1

Sub Main()
Dim cc As New C()
WalkInheritance(cc)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T)
Console.WriteLine(o.GetType().FullName)
WalkInheritance(o, o.GetType().BaseType)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T, ByVal bType
As Type)
If Not bType Is Nothing Then
Console.WriteLine(bType.FullName)
bType = bType.BaseType
WalkInheritance(o, bType)

End If
End Sub

Private MustInherit Class BaseClass

End Class

Private Class A
Inherits BaseClass
End Class

Private Class B
Inherits A
End Class

Private Class C
Inherits B
End Class
End Module

I hope this helps you some...
Tom ... this is very helpful ... thank you very much.

(... and thanks for not questioning my motivations!)

Jun 27 '08 #8
ATM?
FP?

Apart from that, don't worry too much about Cor. He seems to think he's the
hall monitor.
"Simon Woods" <si*********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Cor Ligthert[MVP] wrote:
>Tom,

Are you sure that you are not helping somebody doing his schoolwork?

I see not any practical sense in the questions Simon sends, but it can be
schoolwork.

Cor

In my defense (and perhaps I'm being too defensive)...

1) I'm rather too old for school - I grew up with the Punk Rock.

2) From a practical perspective, as I suggested earlier (and perhaps I'm
wrong), I would have thought that any dependency injection framework would
have come up against this issue - so I'm not sure why you would think it
is 'homework-ish'. I've only perused some of them briefly, but Ninject
certainly does field injection (via tagging in the class itself), so
whether reflection is being used or some other way of going in under the
hood, this issue would surely have come up.

Funnily enough my question reflects a 'real-world' problem. The product I
work on holds a string of data in it's db which is populated into a class
which itself inherits from other classes which inherit ... and so on. ATM
we effectively pass the data into the base class which then takes what it
needs from the data to populate itself, calls a mustoverrides method so
any inheriting class can then take what it needs from the data and pass it
on. Effectively the class is populating itself. I was simply looking for
an alternative way of populating the class using a factory to inject the
data into the fields. The problem I encountered was that the fields
exposed via Reflection's GetFields are only from one particular
inheritance tier. From reading other threads, the suggestion was to walk
the inheritence hierarchy to get at the fields from different tiers.

Perhaps I should have asked how you get at the fields from the different
inheritance tiers using reflection and maybe the answer would have been
different.

3) Wrt my previous question re Lambda expressions, yes I'm trying to learn
as it's all pretty new to me. I find these groups really helpful for my
understanding and I'm likely to post other rather abstract questions about
how to get on in FP. I guess that's just my learning style.

Am I breaching use-net-tiquette?
Jun 27 '08 #9
Stephany Young wrote:
ATM?
At the moment ...
FP?
Functional Programming ...
Sorry about that
Apart from that, don't worry too much about Cor. He seems to think he's
the hall monitor.
Okay ... thx
Jun 27 '08 #10
Stephany,

Apart from that, don't worry too much about Cor. He seems to think he's
the hall monitor.
Sorry that I stepped in your position. I was just trying to help you a
little bit.

Cor

Jun 27 '08 #11
On 2008-06-21, Simon Woods <si*********@hotmail.comwrote:
Tom Shelton wrote:
>On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
>>Tom Shelton wrote:
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:
Hi
>
I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....
>
the generic T is constrainted as the base class of the inheritance hierarchy
>
Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T
>
' .... reflection to set field values at this inheritance tier ...
>
'walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If
>
Return p_def
>
End Function
>
The CType complains that p_def.GetType.BaseType is not defined - why?
when I don't try and cast but simply look at the p_def.GetType.BaseType
at runtime, it's fine.
>
Is there another way to walk the hierarchy?
>
Thx
>
S
Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)
Thx Tom

"Option Strict On disallows implicit conversions from object to T"

(I read elsewhere that there was a problem upcasting generics with
earlier versions of c#. Presumably all the IoCs get round this though)

First, let me apologize... I didn't really understand what you were trying to
accomplish - and the fact is, you can't get there from here - at least not the
way your trying to do it. In other, words you can't dynamically specify the
type of a casst at runtime - and Convert.ChangeType won't help you there...
It basically does stuff like changing strings to numbers, etc - basically it
deals with types that implement IConvertable.

That said, you can definately walk the tree... But your going to have to do
it something like:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Reflection

Module Module1

Sub Main()
Dim cc As New C()
WalkInheritance(cc)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T)
Console.WriteLine(o.GetType().FullName)
WalkInheritance(o, o.GetType().BaseType)
End Sub

Private Sub WalkInheritance(Of T As BaseClass)(ByVal o As T, ByVal bType
As Type)
If Not bType Is Nothing Then
Console.WriteLine(bType.FullName)
bType = bType.BaseType
WalkInheritance(o, bType)

End If
End Sub

Private MustInherit Class BaseClass

End Class

Private Class A
Inherits BaseClass
End Class

Private Class B
Inherits A
End Class

Private Class C
Inherits B
End Class
End Module

I hope this helps you some...

Tom ... this is very helpful ... thank you very much.

(... and thanks for not questioning my motivations!)
Your welcome. And while I try to avoid answering obvious homework questions,
I don't ever remember seeing one involving walking inheritance hiearchies :)
I'm not sure where that accusation came from...

--
Tom Shelton
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
1
by: Keith Patrick | last post by:
I am trying to create a typed collection hierarchy (bunch of placeholders that will eventually be wrappers for generics), but I am having issues with the inheritance. CollectionSubclass :...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
1
by: Zachary Hartnett | last post by:
I was trying to write a routine this morning that would open a given assembly, walk the inheritance tree of classes in the assembly, and provide a list of classes in the assembly that inherit from...
5
by: relient | last post by:
This is rediculous. It is said when you override a method in a derived class that's implemented in the base class, you "override the base class methods" implementation, correct? but in my test,...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.