Differences between revisions 7 and 8
Revision 7 as of 2015-04-05 02:10:21
Size: 1786
Comment:
Revision 8 as of 2015-10-03 12:26:07
Size: 3685
Comment: ByteArray, StringArray and IPAddress added
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
 ⍝ Based on: http://msdn.microsoft.com/en-us/library/y549e41e.aspx   ⍝ Based on: http://msdn.microsoft.com/en-us/library/y549e41e.aspx
Line 33: Line 33:
`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. `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)
Line 41: Line 41:
=== 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[]
Line 42: Line 49:
 ⎕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 prepare 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 if failure

 :If ' '=↑1↑0⍴ip ⍝ Characters ?
     ip←2⊃'.'⎕VFI ip

 :End

 :If 4≠⍴ip ⍝ 4 Numbers ?
     ⎕←'Address is not 4 numbers'
     r←⎕NULL
     →0
 :End

 :Trap 0
     ⎕USING←'System,mscorlib.dll' 'System.Net,System.dll'
     byteArray←Array.CreateInstance(Byte 4)
     {byteArray.Set ⍵}¨↓0 1 2 3,[1.5]ip
     r←⎕NEW IPAddress byteArray

 :Else
   ⍝ There is an error.
     :If 90=⎕EN
         r←⎕NULL
         ⎕←('EXCEPTION: ',⎕EXCEPTION.Message)
     :Else
         r←⎕NULL
         ⎕←((1⊃⎕DM),': ',(2⊃⎕DM))
     :EndIf
 :End
}}}

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 prepare 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 if failure

 :If ' '=↑1↑0⍴ip ⍝ Characters ?
     ip←2⊃'.'⎕VFI ip

 :End

 :If 4≠⍴ip   ⍝ 4 Numbers ?
     ⎕←'Address is not 4 numbers'
     r←⎕NULL
     →0
 :End

 :Trap 0
     ⎕USING←'System,mscorlib.dll' 'System.Net,System.dll'
     byteArray←Array.CreateInstance(Byte 4)
     {byteArray.Set ⍵}¨↓0 1 2 3,[1.5]ip
     r←⎕NEW IPAddress byteArray

 :Else
   ⍝ There is an error.
     :If 90=⎕EN
         r←⎕NULL
         ⎕←('EXCEPTION: ',⎕EXCEPTION.Message)
     :Else
         r←⎕NULL
         ⎕←((1⊃⎕DM),': ',(2⊃⎕DM))
     :EndIf
 :End


CategoryDyalog - CategoryDyalogDotNet - CategoryDyalogDotNetUtilities - CategoryDotNet

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