This Wiki is about sharing snippets of Dyalog Apl code that are useful when using .Net

.Net version installed

GetNetVersion is used for detecting the .Net version installed on the computer.

 ⍝ Function to Get the Latest .Net Framework Installed on this Computer

 ⍝ Based on:
 ⍝ v = Latest Version Installed
 ⍝ v = 0, When There is no Version of .Net Installed

 :Trap 0
     ⎕USING←'System,mscorlib.dll' 'System.IO,mscorlib.dll'
     DI←⎕NEW DirectoryInfo(⊂dir)             ⍝ Get the DirectoryInfo Object for the Directory
     dirs←DI.GetDirectories(⊂'v*')           ⍝ v* is the filter
     dirs←1↓¨dirs.Name                       ⍝ To remove the letter 'v' at the beginning of each directory
     v←+/¨(⊂1 0.1)ר2↑¨⊃¨(//¨'.'⎕VFI¨dirs)   ⍝ To Add the first 2 numbers of each directory
     v←1↑∪v[⍒v]                              ⍝ To order the directory names as numbers and return the highest number
    ⍝ There is no .Net installed

Close the Application Domain

CloseAppDomain is the equivalent to File --> CloseAppDomain. It is used to remove the dll loaded in the memory of the interpreter. Usefull when experimenting with .Net. (From version 14.1 there is a I beam for that now: 2101⌶0)

⍝ To erase all the loaded dll from memory.
 'Closing AppDomain...'
 1 ⎕NQ'⎕SE.mb.file.closead' 'Select'
 'AppDomain Closed'

Byte Array: Byte[]

If the constructor needs a Byte Array you can use the following function to build it:

 ba←ByteArray bytes;⎕IO;⎕USING
⍝ Function to get a .Net byte array
⍝ bytes = numbers in the range of 0 to 255
⍝ ba    = .Net Byte[]

 ⎕USING ⎕IO←'System,mscorlib.dll' 0
 ba←Array.CreateInstance(Byte(⍴,bytes))  ⍝ Empty array of type 'Byte'
 {ba.Set ⍵}¨↓(⍳⍴,bytes),[0.5]bytes       ⍝ Populate the Byte array

String Array: String[]

If the constructor needs a String Array you can use the following function to build it:

 sa←StringArray strings;⎕USING;⎕IO
⍝ Function to get a .Net string array
⍝ strings = characters
⍝ sa      = .Net String[]

 ⎕USING ⎕IO←'System,mscorlib.dll' 0
 sa←Array.CreateInstance(String(⍴,strings))        ⍝ Empty array of type 'String'
 {sa.Set(⍵[0](,⍵[1]))}¨↓(⍳⍴,strings),[0.5]strings  ⍝ Populate the String array


The following function construct a .Net IPAddress:

 r←IP ip;byteArray;⎕USING
⍝ ip = IP address as 4 numbers or characters (like '')
⍝ r  = IPAddress .net object if success or ⎕NULL (error) if failure

 :Trap 0

     :If ' '=↑1↑0⍴ip ⍝ Characters ?
         r←⎕NEW IPAddress(ip.Address)

     :Else ⍝ Numbers
         r←⎕NEW IPAddress(⊂,ip)


   ⍝ There is an error.
     :If 90=⎕EN
         r←⎕NULL('EXCEPTION: ',⎕EXCEPTION.Message)

         r←⎕NULL((1⊃⎕DM),': ',(2⊃⎕DM))


Cleaning and Saving the Workspace

While using heavily .Net you may end-up with a lot of dead instances of .Net objects in the Workspace. The following function CleanAndSave will erase all the .Net objects of the Workspace, compact the Workspace and check it's integrity before Saving. In the unlikely hood of a problem, it will suggest a way to Save the Workspace as Text using SALT.Snap and to reconstruct it using SALT.Boot. While developing your .Net application it is good practice to use this function to do some 'housekeeping' of the Workspace.

⍝ Utility function to Save a WS while heavily using .Net objects.
⍝ It will remove all the .Net objects, compact and check the integrity of the WS.
⍝ If there is a problem it will suggest a way to try to recuperate the WS using SALT.
⍝ Requirements: v14.1 or better.

⍝ 1. Close AppDomain

⍝ 2. Erase all the .Net objects (adapted from a post on the forum from Phil Last)
 ∆refs←{                             ⍝ Vector of sub-space references for ⍵.
     ⍺←⍬ ⋄ (⍴,⍺)↓⍺{                  ⍝ default exclusion list.
         ⍵∊⍺:⍺                       ⍝ already been here: quit.
         ⍵.(↑∇∘⍎⍨/⌽(⊂⍺∪⍵),↓⎕NL 9.1)  ⍝ recursively traverse any sub-spaces.
     }⍵                              ⍝ for given starting ref.

 {⍵.⎕EX ⍵.⎕NL-9.2}∆refs #

⍝ 3. Clean-Up and Compact the WS

⍝ 4. Check integrity of WS (Contributed by Dyalog)
 dflags←2 ⎕NQ'.' 'setdflags' 417

 :If 0=2 ⎕NQ'.' 'wscheck'
    ⍝ WS integrity is OK
    ⍝ WS integrity is not OK

 2 ⎕NQ'.' 'setdflags'dflags

⍝ 5. Save the WS
 :If integrity
    ⍝ Save the WS to ⎕WSID location without the state indicator.
     0 ⎕SAVE ⎕WSID
     ⎕←'WS Saved: ',⎕WSID

    ⍝ There is a problem with the WS integrity.
     ⎕←'WS NOT Saved and may be corrupted.'
     ⎕←'Try Saving the WS as Text using SALT.Snap and Reload-it using SALT.Boot'
     '⎕SE.SALT.Snap ''',⎕WSID,'_Snap -loadfn -makedir'' ⍝ Save the WS as text'
     '⎕SE.SALT.Boot ''',⎕WSID,'_Snap\load_ws.dyalog -xload'' ⍝ Reload the WS saved as text'
     '⍝ Inspect the WS if complete and re-save it'


CategoryDyalog - CategoryDyalogDotNet - CategoryDyalogDotNetUtilities - CategoryDotNet

netVaria (last edited 2015-10-13 16:05:19 by PierreGilbert)