Reflection Problem

Reflection Problem

Old forum URL: forums.lhotka.net/forums/t/3762.aspx


stanc posted on Monday, October 22, 2007

I am having problems accessing a property of an object becasue of late binding. I know what the base of the object is, but because that base is based of the CSLA ROB it uses reflection to pass the object to itself. In any case I cannot figure out how to access the property if I do not know the exact implementation of the base being used. I figured a short example might help explain what I am looking to do. I know this isn't a CSLA specific question, but I was hoping someone on this forum could help.

Thanks

'------------------SUB CLASS

<Serializable()> _

Public MustInherit Class ColorBase(Of T As ColorBase(Of T))

Inherits Csla.ReadOnlyBase(Of T)

Private _Hue As String

Public Overridable Property Hue() As String

Get

Return _Hue

End Get

Friend Set(ByVal value As String)

_Hue = value

End Set

End Property

End Class

 

'--------------IMPLEMENTATIONS

Public Class BlueColor

Inherits ColorBase(Of BlueColor)

Protected Overrides Function GetIdValue() As Object

Return Nothing

End Function

End Class

 

Public Class RedColor

Inherits ColorBase(Of RedColor)

Protected Overrides Function GetIdValue() As Object

Return Nothing

End Function

End Class

 

'--------------PROBLEM

Public Sub GetHue(ByVal o As Object)

'I know o is from the ColorBase, so I know it has a property Hue

'but I do not know if it is BlueColor or RedColor. How can I

'access the Hue property if Option Strict is ON? Also keep in mind

'that although in this example I have control over what o is in the declaration,

'in my actually situtation I can not change the fact that it is set as

'an Object. I would like to do some sort of DirectCast to the ColorBase, but I

'can't figure out the syntax.

End Sub

McManus replied on Monday, October 22, 2007

Hi stanc,

Probably the best way to do this is to define an interface (for instance IColor) that defines the Hue property. The interface must be implemented by ColorBase(Of T). In the GetHue method you can cast object o to IColor. Now the Hue property is available.

Hope this helps!

Cheers,
Herman

stanc replied on Monday, October 22, 2007

That is exactly what I needed! I must be going brain dead! Thank you for the response.

Thanks,

Stanley

Copyright (c) Marimer LLC