EDN Admin
Well-known member
I am currently attempting to develop a semi-open source commercial game project, which isnt a game just yet because it is in very early development. When I say attempting, I mean I have a problem when attempting to use Reflection to invoke a member when passing an argument using CCore.Screen, a class the member requires as one of its arguments.
I assumed that the arguments to pass was the Screen class found in the MainForm, so here is the code of the MainForm:Imports System.Reflection
Public Class MainForm
Private _toolBox As Tools
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Get initialiser text file
Dim objLocation As String = My.Computer.FileSystem.ReadAllText("Initialise.txt")
Dim contents() As String = objLocation.Split(vbCrLf)
Dim InstanceClass As String = Nothing
For Each statement As String In contents
Select Case statement
Case Files.LookupString(statement, "Initialise")
Dim words As Files.TextContainer = Files.SplitText(statement, " ")
If words(1) = "Class" Then
InstanceClass = words(2)
End If
End Select
Next
Dim objAssemby As Assembly = Reflection.Assembly.LoadFrom("menus.dll")
Dim initialise As Object = objAssemby.CreateInstance(InstanceClass)
Dim params(0) As Object
params(0) = Me.MainScreen
If Not initialise Is Nothing Then
initialise.GetType.InvokeMember("Initialise", BindingFlags.InvokeMethod, Nothing, initialise, params)
End If
End Sub
Private Sub MainForm_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd, MyBase.SizeChanged
If Me.WindowState = FormWindowState.Normal Then
MainScreen.Size = (New Point(Me.Width - 16, Me.Height - 38))
End If
End Sub
End Class
Then, if all goes well, the code that it should execute would be that of below in menus.MainMenu:Imports CCore
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainMenu
Public Class MenuGObjectCollection
Inherits Generic.List(Of GObject)
Private _background As Image
Public Property Background() As Image
Get
Return _background
End Get
Set(value As Image)
_background = value
End Set
End Property
End Class
Public Property Objects() As New MenuGObjectCollection
Public Function UpdateScreen() As MenuGObjectCollection
Objects.AddRange(New GObject() {Header()})
Return Objects
End Function
Header
Private _header As GObject
Public Overridable Function Header() As GObject
_header.Alignment = GObject.Align.TopLeft
_header.BackColor = Drawing.Color.Red
_header.SizeX = 1
_header.SizeY = 0.2
Return _header
End Function
Public Sub Initialise(screen As CCore.Screen)
Dim items As MenuGObjectCollection = UpdateScreen()
For i As Integer = 0 To Objects.Count - 1
screen.Controls.Add(items(i))
Next
End Sub
End Class
From the first code block, if all goes well in the loading of the form, the Initialise(screen As CCore.Screen) should be executed and things should happen. However, does not seem to work the way I want to.
I used breakpoints and stepped through the code, and ignored the final piece of code, skipping the entirety of invoking the member. I am still getting used to the basics of Reflection, so there is probably something obvious I am missing in order to complete this.
You can find this project here if you wish to download and reproduce the error efficiently.
I thank you for any help.
View the full article
I assumed that the arguments to pass was the Screen class found in the MainForm, so here is the code of the MainForm:Imports System.Reflection
Public Class MainForm
Private _toolBox As Tools
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Get initialiser text file
Dim objLocation As String = My.Computer.FileSystem.ReadAllText("Initialise.txt")
Dim contents() As String = objLocation.Split(vbCrLf)
Dim InstanceClass As String = Nothing
For Each statement As String In contents
Select Case statement
Case Files.LookupString(statement, "Initialise")
Dim words As Files.TextContainer = Files.SplitText(statement, " ")
If words(1) = "Class" Then
InstanceClass = words(2)
End If
End Select
Next
Dim objAssemby As Assembly = Reflection.Assembly.LoadFrom("menus.dll")
Dim initialise As Object = objAssemby.CreateInstance(InstanceClass)
Dim params(0) As Object
params(0) = Me.MainScreen
If Not initialise Is Nothing Then
initialise.GetType.InvokeMember("Initialise", BindingFlags.InvokeMethod, Nothing, initialise, params)
End If
End Sub
Private Sub MainForm_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd, MyBase.SizeChanged
If Me.WindowState = FormWindowState.Normal Then
MainScreen.Size = (New Point(Me.Width - 16, Me.Height - 38))
End If
End Sub
End Class
Then, if all goes well, the code that it should execute would be that of below in menus.MainMenu:Imports CCore
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainMenu
Public Class MenuGObjectCollection
Inherits Generic.List(Of GObject)
Private _background As Image
Public Property Background() As Image
Get
Return _background
End Get
Set(value As Image)
_background = value
End Set
End Property
End Class
Public Property Objects() As New MenuGObjectCollection
Public Function UpdateScreen() As MenuGObjectCollection
Objects.AddRange(New GObject() {Header()})
Return Objects
End Function
Header
Private _header As GObject
Public Overridable Function Header() As GObject
_header.Alignment = GObject.Align.TopLeft
_header.BackColor = Drawing.Color.Red
_header.SizeX = 1
_header.SizeY = 0.2
Return _header
End Function
Public Sub Initialise(screen As CCore.Screen)
Dim items As MenuGObjectCollection = UpdateScreen()
For i As Integer = 0 To Objects.Count - 1
screen.Controls.Add(items(i))
Next
End Sub
End Class
From the first code block, if all goes well in the loading of the form, the Initialise(screen As CCore.Screen) should be executed and things should happen. However, does not seem to work the way I want to.
I used breakpoints and stepped through the code, and ignored the final piece of code, skipping the entirety of invoking the member. I am still getting used to the basics of Reflection, so there is probably something obvious I am missing in order to complete this.
You can find this project here if you wish to download and reproduce the error efficiently.
I thank you for any help.
View the full article