azurefx / DotNET.jl

Julia ❤️ .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling methods with optional arguments

cserteGT3 opened this issue · comments

I'm not really familiar with the internals of the package (nor .Net to be honest), but would it be possible to enable calling methods without specifyin optional arguments?
I copied and modified an MWE from a previous issue:

PS C:\Users\csert\Documents\temp\dotnettest> Add-Type -Namespace JuliaTest -Name Foo -MemberDefinition @'
>> public double Bar(double x, double y = 2)
>> {
>> return y*x;
>> }
>> '@ -OutputAssembly Example.dll

Calling from julia:

julia> using DotNET

julia> asm=T"System.Reflection.Assembly".LoadFrom("Example.dll")
System.Reflection.RuntimeAssembly("Example, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")

julia> Foo=asm.GetType("JuliaTest.Foo")
JuliaTest.Foo

julia> foo = Foo.new()
JuliaTest.Foo("JuliaTest.Foo")

julia> foo.Bar(3.14, 2)
6.28

julia> foo.Bar(3.14, 3)
9.42

julia> foo.Bar(3.14)
ERROR: CLRException: System.MissingMethodException: Method 'JuliaTest.Foo.Bar' not found.
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at CLRBridge.Meta.InvokeMember(IntPtr type, String name, BindingFlags bindingFlags, IntPtr binder, IntPtr target, IntPtr[] providedArgs, UInt32 argCount, IntPtr& exception)
Stacktrace:
 [1] track_and_throw(exhandle::UInt64)
   @ DotNET.CLRBridge C:\Users\csert\.julia\packages\DotNET\Rx490\src\CLRBridge.jl:46
 [2] InvokeMember(type::UInt64, name::String, bindingFlags::UInt32, binder::Int64, target::UInt64, providedArgs::Vector{UInt64})
   @ DotNET.CLRBridge C:\Users\csert\.julia\packages\DotNET\Rx490\src\CLRBridge.jl:325
 [3] invokemember(flags::UInt32, type::CLRObject, this::CLRObject, name::Symbol, args::Float64)
   @ DotNET C:\Users\csert\.julia\packages\DotNET\Rx490\src\marshalling.jl:83
 [4] invokemember(type::CLRObject, this::CLRObject, name::Symbol, args::Float64)
   @ DotNET C:\Users\csert\.julia\packages\DotNET\Rx490\src\marshalling.jl:94
 [5] (::DotNET.PendingInvocation)(args::Float64)
   @ DotNET C:\Users\csert\.julia\packages\DotNET\Rx490\src\operators.jl:19
 [6] top-level scope
   @ REPL[7]:1

Unfortunately, C# optional parameters are not currently supported.

Optional parameters are handled by the compiler, and this method still has two parameters at the IL level, so you need to specify both parameters.

This package uses reflection to call the target method, so it may be possible to implement optional parameters through a custom Binder. Will consider doing this if I have spare time.

Thank you for the answer!