Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling methods with optional arguments #22

Open
cserteGT3 opened this issue Aug 18, 2023 · 2 comments
Open

Calling methods with optional arguments #22

cserteGT3 opened this issue Aug 18, 2023 · 2 comments

Comments

@cserteGT3
Copy link

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
@azurefx
Copy link
Owner

azurefx commented Aug 20, 2023

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.

@cserteGT3
Copy link
Author

Thank you for the answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants