Introspection invoke with array as parameters

Hello,

I have an issue understanding something using invoke

I have a shared method that take the following as parameters

pWindow as DesktopWindow, pSelectedEntity as User = nil, optional pEntitiesToLoad() as User = nil, optional pEntitiesToExclude() as User = nil, pFilters as Dictionary = nil

I have this method on different classes that I use and I want to invoke this method using introspection but I got illegalCastException regarding the 2 optional arrays

This code does not work

var tClass as Introspection.TypeInfo = GetTypeInfo(User)
if tClass<>nil then
  var tMethods() as Introspection.MethodInfo = tClass.GetMethods()
  for each tMethod as introspection.MethodInfo in tMethods
    if tMethod.IsShared and tMethod.Name = "pick" then
      var tParams() as introspection.ParameterInfo = tMethod.GetParameters()
      var params() as Variant
      params.Add(self)
      params.add(nil)
      params.add(nil)
      params.add(nil)
      params.add(nil)
      tEntity=tMethod.Invoke(nil,params)
      exit
    end if
  next
end if

If I replace some params by this, its working

var params() as Variant
params.Add(self)
params.add(nil)
var tArray(-1) as User
params.add(tArray)
params.add(tArray)
params.add(nil)

but if I replace the array declaration by:

var tArray(-1) as User

It does not work anymore.

I would like to have this code working regardless of the class that I use or is it an issue with introspection?

Thank you

I don’t know a way around this other than to overload your methods:

Sub SharedMethod(pWindow As DesktopWindow)
  SharedMethod(pWindow, nil, nil, nil)
End Sub

Sub SharedMethod(pWindow As DesktopWindow, pSelectedEntity As User, ...)
  // Do something
End Sub

When you don’t want to supply the additional parameters, you would find the method that takes only one parameter.

BTW, you can use the Optional keyword or assign a default value, you don’t need to do both (even though that works).

Thank you, I will try that when I come back to my mac