bcc icon indicating copy to clipboard operation
bcc copied to clipboard

Interface type parameters do not accept objects when using TMethod.Invoke

Open DivineDominion opened this issue 5 years ago • 2 comments

Bug Report

  • Declare an Interface, e.g. Foo
  • Declare a type conforming to Foo, e.g. FooImpl
  • Passing an object of FooImpl to a function expecting a parameter of type Foo works, but using brl.reflection to pass the same object via TMethod.Invoke produces an error

Example code:

Interface AnInterface
End Interface

Type AType Implements AnInterface
End Type

Type AnEventListener
    Method OnEventSent(source:AnInterface) ' use :AType or :Object to fix
        Print("OnEvent")
    End Method
End Type

Global listener:AnEventListener = New AnEventListener()

Function Fire(eventName:String, source:Object, payload:Object = Null)
    Local callbackName:String = "On" + eventName
    DebugLog("Firing " + eventName)
    Local callback:TMethod = TTypeId.ForObject(listener).FindMethod(callbackName)
    If callback
        callback.Invoke(listener, [source]) ' Produces "ERROR" message during runtime
    End If
End Function

Local obj:AType = New AType()
Fire("EventSent", obj)

Expected Behavior

The method is invoked and executed with the object available inside.

Actual Behavior

Alert with Unhandled Exception:ERROR during runtime as long as the method parameter's type is the one of an interface. Works with concrete classes or Object, though.

Environment

  • Operating System: macOS 10.14
  • Output of bcc -v: 0.99 release

DivineDominion avatar May 01 '19 07:05 DivineDominion