Contents
netVaria
Overview
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.
v←GetNetVersion;DI;dir;dirs;⎕USING ⍝ Function to Get the Latest .Net Framework Installed on this Computer ⍝ Based on: http://msdn.microsoft.com/en-us/library/y549e41e.aspx ⍝ ⍝ v = Latest Version Installed ⍝ v = 0, When There is no Version of .Net Installed :Trap 0 ⎕USING←'System,mscorlib.dll' 'System.IO,mscorlib.dll' dir←Environment.ExpandEnvironmentVariables(⊂'%WINDIR%\Microsoft.NET\Framework') 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 :Else ⍝ There is no .Net installed v←0 :EndTrap
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)
CloseAppDomain ⍝ 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
IPAddress
The following function construct a .Net IPAddress:
r←IP ip;byteArray;⎕USING ⍝ ip = IP address as 4 numbers or characters (like '192.168.1.1') ⍝ r = IPAddress .net object if success or ⎕NULL (error) if failure :Trap 0 ⎕USING←'System.Net,System.dll' :If ' '=↑1↑0⍴ip ⍝ Characters ? ip←IPAddress.Parse(⊂,ip) r←⎕NEW IPAddress(ip.Address) :Else ⍝ Numbers r←⎕NEW IPAddress(⊂,ip) :End :Else ⍝ There is an error. :If 90=⎕EN r←⎕NULL('EXCEPTION: ',⎕EXCEPTION.Message) :Else r←⎕NULL((1⊃⎕DM),': ',(2⊃⎕DM)) :EndIf :End
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.
CleanAndSave;∆refs;dflags;integrity ⍝ 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 {}2101⌶0 ⍝ 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 {}⎕WA ⍝ 4. Check integrity of WS (Contributed by Dyalog) dflags←2 ⎕NQ'.' 'setdflags' 417 :If 0=2 ⎕NQ'.' 'wscheck' ⍝ WS integrity is OK integrity←1 :Else ⍝ WS integrity is not OK integrity←0 :EndIf 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 :Else ⍝ 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' '' ']CLEAN' '⎕SE.SALT.Snap ''',⎕WSID,'_Snap -loadfn -makedir'' ⍝ Save the WS as text' ')CLEAR' '⎕SE.SALT.Boot ''',⎕WSID,'_Snap\load_ws.dyalog -xload'' ⍝ Reload the WS saved as text' ']CLEAN' '⍝ Inspect the WS if complete and re-save it' :End
CategoryDyalog - CategoryDyalogDotNet - CategoryDyalogDotNetUtilities - CategoryDotNet