How do I force a call to a specific .Net function overload?
Forum rules
The FAQ is a read-only forum which is in general updated only by employees of Dyalog Ltd. It replaces the FAQ page which existed under http://www.dyalog.com. Rather than rejecting other posts to this forum, such posts will be put in a moderation queue, and moved to a more appropriate forum.
The FAQ is a read-only forum which is in general updated only by employees of Dyalog Ltd. It replaces the FAQ page which existed under http://www.dyalog.com. Rather than rejecting other posts to this forum, such posts will be put in a moderation queue, and moved to a more appropriate forum.
1 post
• Page 1 of 1
How do I force a call to a specific .Net function overload?
Dyalog does a pretty good job of figuring out which function overload to call based on the arguments you give to the function. However, in some cases (or just out of interest) it's useful to be able to call a specific overload. The following function takes an array of Types in the argument and matches those types to an overload before invoking the appropriate function.
(Note that all these examples work perfectly well with Dyalog's normal overload resolution.
Interesting (?) test cases are:
Note that the last example formats the string using Spanish for the day of the week.
Of course the first part of the function can be used to determine what overloads are available, and if a niladic version of the function exists.
John Daintree.
(Note that all these examples work perfectly well with Dyalog's normal overload resolution.
- Code: Select all
r←obj InvokeOverload(name types args);fns;params;overload;flags
flags←BindingFlags.Public+BindingFlags.Instance ⍝ only interested in public instance
:If name≡'.ctor' ⍝ invoking []NEW?
fns←(Type.GetType⊂obj).GetConstructors flags ⍝ find all fns with name
args←,⊂args
:Else ⍝ and instance as an argument
fns←obj.GetType.FindMembers MemberTypes.Method flags Type.FilterName name ⍝ find named methods
args←obj args ⍝ arguments include instance
:EndIf
params←{0::⍬ ⋄ ⍵.ParameterType}¨fns.GetParameters ⍝ find all parameter lists
overload←((⊂,types)≡¨params)/fns ⍝ and find correct overload
:If 1=⍴overload ⍝ find one?
r←(⊃overload).Invoke args ⍝ invoke the function
:EndIf
Interesting (?) test cases are:
- Code: Select all
test;e;dt123;c;⎕USING
⎕USING←'System' 'System.Reflection'
'System.Exception'InvokeOverload'.ctor'⍬ ⍬
'System.Exception'InvokeOverload'.ctor'String(,⊂'how unfortunate')
dt123←'System.DateTime'InvokeOverload'.ctor'(6⍴Int32)(1965 8 31 1 2 3)
dt123
dt123 InvokeOverload'ToString'⍬ ⍬
dt123 InvokeOverload'ToString'String(,⊂'(dddd) yyyy+dd+MM hh+mm+ss')
c←⎕NEW Globalization.CultureInfo(⊂'es-ES')
dt123 InvokeOverload'ToString'(String IFormatProvider)('(dddd) yyyy+dd+MM hh+mm+ss'c.DateTimeFormat)
Note that the last example formats the string using Spanish for the day of the week.
Of course the first part of the function can be used to determine what overloads are available, and if a niladic version of the function exists.
John Daintree.
-
JohnD|Dyalog - Posts: 74
- Joined: Wed Oct 01, 2008 9:35 am
1 post
• Page 1 of 1
Return to Frequently Asked Questions (FAQ)
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group