How do I use InvokeOverload for.NET Generic class instances?

Using (or providing) Microsoft.NET Classes

How do I use InvokeOverload for.NET Generic class instances?

Postby RossHale on Sat Jan 22, 2011 1:00 am

Instances of .NET Generic classes are usefull for strongly typed collections of data.
The .NET System.Collections namespace provides useful collections of data where the items may be of mixed types.
You can use Dyalog ⎕NEW to directly construct instances of these collections.
The .NET System.Collections.Generic namespace provides more varieties of collections where the items are constrained
to specific types.

I don't think you can use Dyalog ⎕NEW for these Generic types due to their names not mapping to APL.
You can of course use C# assembly dll's to specify Generic types with a relationship to a name that can be used
in Dyalog APL with a ⎕USING statement to make the C# assembly dll available.

I thought, wrongly, that the C# dll path was the only way to get Generic types into Dyalog APL.
I believed that the C# compiler was necessary to instantiate Generic types and that it could not be done on the fly.

Thanks to Andy's InvokeOverload, Generic types can be constructed on the fly!

It is neccessary to use the behind the scenes, "mangled" name for the Generic type...
You take the C# style type name such as List<System.Int32> in its fully qualified form
Code: Select all
'System.Collections.Generic.List<System.Int32>'   ⍝ for C#

'System.Collections.Generic.List`1[System.Int32]' ⍝ mangled form. `1 for one specified type

mylist←'System.Collections.Generic.List`1[System.Int32]' InvokeOverload'.ctor' ⍬ ⍬

mydictionary←'System.Collections.Generic.Dictionary`2[System.String,System.Int32]' InvokeOverload'.ctor' ⍬ ⍬


These examples are for null argument constructors. You can supply a typelist and arguments for other constructors.

I made a few changes to InvokeOverload. My setup seemed to want fully qualified names like System.Type instead of Type.
I also made a change for how the InvokeOverload dealt with a null typelist for construction.

I am using 64-bit Windows 7 Professional and Dyalog APL Unicode 32-bit with .NET 4.0.

Code: Select all
r←obj InvokeOverload(name types args);fns;params;overload;flags
 ⍝⍝ Ross Hale -- slight modifications to fully qualify names such as System.Type instead of Type ..
 flags←System.Reflection.BindingFlags.Public+System.Reflection.BindingFlags.Instance
 ⍝ only interested in public instance
 :If name≡'.ctor' ⍝ invoking []NEW?
     fns←(System.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 System.Type.FilterName name ⍝ find named methods
     args←obj args ⍝ arguments include instance
 :EndIf
 params←{0::⍬ ⋄ ⍵.ParameterType}¨fns.GetParameters ⍝ find all parameter lists
 :If 0=⍴,types
     overload←(0=∊⍴¨params)/fns ⍝ select overload with no parameters
 :Else
     overload←((⊂,types)≡¨params)/fns ⍝ and find correct overload
 :EndIf
 :If 1=⍴overload ⍝ find one?
     r←(⊃overload).Invoke args ⍝ invoke the function
 :EndIf


Code: Select all
↑ ⎕using
                                                                                               
System,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll                               
System,C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll                                 
System.Collections,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll                   
System.Reflection,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll                   
System.Reflection.Cache,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll             
System.Reflection.Emit,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll               
System.Collections.Generic,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll           
System.Collections.Generic,C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll             
System.Collections.Generic,C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Core.dll       
System.Collections.Generic,C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll
User avatar
RossHale
 
Posts: 31
Joined: Thu Jun 18, 2009 9:08 pm
Location: Bellevue, Washington, USA

Return to Microsoft.NET

Who is online

Users browsing this forum: No registered users and 1 guest