How do I force a call to a specific .Net function overload?

The Dyalog "Knowledgebase". This forum is read-only
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 Rather than rejecting other posts to this forum, such posts will be put in a moderation queue, and moved to a more appropriate forum.

How do I force a call to a specific .Net function overload?

Postby JohnD|Dyalog on Fri Nov 05, 2010 4:23 pm

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.

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               
     :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           
     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                 

Interesting (?) test cases are:

Code: Select all
⎕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 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.
User avatar
Posts: 68
Joined: Wed Oct 01, 2008 9:35 am

Return to Frequently Asked Questions (FAQ)

Who is online

Users browsing this forum: No registered users and 1 guest