我已经看到了一些其他的回应,他们谈论的接口,但我敢肯定,你可以做到这一点的类和基类,但我不能这个工作。.net动态加载
Public Class Behavior
Private _name As String
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
Public Property EditorUpdate As Boolean
Public Sub New(ByVal name As String)
_name = name
EditorUpdate = False
End Sub
Public Overridable Sub Update()
End Sub
' runs right away in editor mode. also runs when in stand alone game mode right away
Public Overridable Sub Start()
End Sub
' runs after game mode is done and right before back in editor mode
Public Overridable Sub Finish()
End Sub
' runs right when put into game mode
Public Overridable Sub Initialize()
End Sub
' runs when the game is complete in stand alone mode to clean up
Public Overridable Sub Destroy()
End Sub
末级
Public Class CharacterController
Inherits Behavior.Behavior
Public Sub New()
MyBase.New("Character Controller")
End Sub
Public Overrides Sub Update()
' TODO: call UpdateController()
' THINK: how can UpdateController() get the controller entity it's attached to?
' Behaviors need a way to get the entity they are attached to. Have that set when it's assigned in the ctor?
End Sub
末级
Dim plugins() As String
Dim asm As Assembly
plugins = Directory.GetFileSystemEntries(Path.Combine(Application.StartupPath, "Plugins"), "*.dll")
For i As Integer = 0 To plugins.Length - 1
asm = Assembly.LoadFrom(plugins(i))
For Each t As Type In asm.GetTypes
If t.IsPublic Then
If t.BaseType.Name = "Behavior" Then
behaviorTypes.Add(t.Name, t)
Dim b As Behavior.Behavior
b = CType(Activator.CreateInstance(t), Behavior.Behavior)
'Dim o As Object = Activator.CreateInstance(t)
End If
End If
Next
Next
当它试图转换任何Activator.CreateInstance(T)返回到基类类型的行为我越来越无效抛出异常。该类型应该是CharacterController,它被定义为Behavior的子元素,所以为什么它不让我进行该操作?我之前做过这样的事情,但是我找不到我的代码。我错过了什么?
也许使用'DirectCast(Activator.CreateInstance(t),Behavior)' – IAbstract 2011-01-06 01:46:10
是的,我试了两次,它仍然没有工作。 :/ – user441521 2011-01-06 01:54:54
也许你应该检查返回的对象的类型,看看它到底是什么。 – cdhowie 2011-01-06 01:56:15