繁体   English   中英

.net动态加载

[英].net dynamic loading

我已经看到了其他一些关于此的响应,他们谈论了接口,但是我敢肯定,您可以使用类和基类来做到这一点,但我不能这样做。

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)返回到Behavior类型的基类时,我得到了无效的强制转换异常。 该类型应该是CharacterController,它被定义为Behavior的子类,所以为什么不让我强制转换呢? 我之前已经做过类似的事情,但是我找不到我的代码。 我想念什么?

这可能不是您问题的答案(它也可能会解决您的异常-谁知道),但这是需要指出的问题。 这些行:

If t.IsPublic Then
    If t.BaseType.Name = "Behavior" Then

实际上应该更改为这样的一个条件:

If t.IsPublic AndAlso (Not t.IsAbstract) AndAlso _
    GetType(Behavior.Behavior).IsAssignableFrom(t) Then

否则,如果有人在自己的程序集中定义了一个称为“行为”的随机类型,并从另一个类型派生它,则您的代码将认为它是一个插件。 此外,如果有人派生您的“ Behavior类型,然后派生该类型(两个继承级别),则此代码将错误地跳过该类型。 使用IsAssignableFrom方法是一种快速简便的方法,即使在继承树中的类型之间存在另一种类型,也可以确保一种类型确实从所需的特定类型派生(而不是共享相同名称的任何类型)。 。 t.IsAbstract的附加检查还将确保您不尝试实例化基本插件类型的抽象子类型。

这对我有用:

             Dim ctor As Reflection.ConstructorInfo = _
                t.GetConstructor(New System.Type() {})
             Dim o As Object = ctor.Invoke(New Object() {})
             Dim plugin As Plugin = TryCast(o, Plugin)

(如果找到t ,则调用无参数构造函数。)

[我只是意识到这可能是Activator.CreateInstance所做的,所以我用您的代码替换了我的代码,它按照您的方式工作了-所以这可能对您没有帮助]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM