Walking inheritance hierarchy - why doesn'y this work?

S

Simon Woods

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
 
S

Simon Woods

Tom said:
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)
 
C

Cor Ligthert[MVP]

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
 
S

Simon Woods

Cor said:
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?
 
S

Simon Woods

Tom said:
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!)
 
S

Stephany Young

ATM?
FP?

Apart from that, don't worry too much about Cor. He seems to think he's the
hall monitor.
 
C

Cor Ligthert[MVP]

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
 

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