I've got a question about inheritance.
I've got a bunch of classes that get reflected into a larger project dynammically at run time. All the classes need to have a standard set of methods and properties to interact with this larger project and a lot of overhead code is similar between the classes, so I have a base class that handles the properties and methods, and then a child class that takes care of the specific code for each block via inheritance.
Here's the problem:
In the base class, there is a property called "Memory" declared as an Object. This is used inside the child class to store data, and it needs to be able to contain any type of data, so I have a class called "InternalMemory" which that Memory property gets converted to. This needs to happen the same way for all my child classes, so I'd like to handle it in the base class, but the type it gets converted to needs to be defined in the child class. Is there any way to do this?
Here's some simplified example code.
Public MustInherit Class BlockOverhead
Public Property Memory As Object
Get
Return BlockMemory
End Get
Set(value As Object)
BlockMemory = value
End Set
End Property
Protected BlockMemory As New Object
Public Sub TryExecute()
'==========================================================================
'Try to import the block memory
'==========================================================================
InternalBlockMem = TryCast(BlockMemory, Object.GetType(InternalBlockMem))
If IsNothing(InternalBlockMem) Then
InternalBlockMem = New InternalMemory
End If
'==========================================================================
'Try to execute the block
'==========================================================================
Execute()
'==========================================================================
'Save the block memory
'==========================================================================
BlockMemory = New Object
BlockMemory = InternalBlockMem
End Sub
Protected MustOverride Sub Execute()
End Class
Public Class Magic1
Inherits BlockOverhead
Public Class InternalMemory
'Create block's memory vars here.
Public OutValue1 As Integer
Public OutValue2 As Boolean
End Class
Protected Shadows InternalBlockMem As New InternalMemory
Protected Overrides Sub Execute()
'Do some stuff to InternalblockMemory
InternalBlockMem.OutValue1 = 0
InternalBlockMem.OutValue2 = True
End Sub
End Class
Public Class Magic2
Inherits BlockOverhead
Public Class InternalMemory
'Create block's memory vars here.
Public Test As New List(Of String)
Public Value As Decimal
End Class
Protected Shadows InternalBlockMem As New InternalMemory
Protected Overrides Sub Execute()
'Do some stuff to InternalblockMemory
InternalBlockMem.Test.Clear()
InternalBlockMem.Test.Add("Stuff")
InternalBlockMem.Value = InternalBlockMem.Value + 1
End Sub
End Class
What I have tried:
I've tried shadowing, but formatting in the base class modified the base class instance of the variable.
Tried google for several hours. No luck there either.