Calling .NET Libraries from Dyalog APL using the ⎕NET Interface
Learn how to call .NET libraries from Dyalog APL using the ⎕NET interface, with a concrete example that builds a Windows Form and handles events in APL.
01 Jul 2025, 01:54 UTC

Problem: Need .NET functionality inside an APL workflow
\nMany organizations run Dyalog APL for data analysis or automation but occasionally require a Windows‑only .NET component—such as a UI library, a cryptographic provider, or a legacy COM wrapper. Rewriting that code in APL is often impractical, so a reliable way to invoke .NET types directly from the APL session is needed.
\nThesis: The ⎕NET system function provides a straightforward, version‑stable bridge
\nStarting with Dyalog APL 18.0, the ⎕NET function loads a .NET assembly, exposes its types as APL namespaces, and handles automatic marshalling of primitive data. This lets you treat .NET classes almost like APL objects while staying within a single session.
\nSetup and loading an assembly
\nFirst, ensure you are running Dyalog on Windows (the ⎕NET interface is not available on Linux or macOS). Choose a .NET version that matches the Dyalog runtime; the documentation recommends .NET Framework 4.8 or .NET Core 3.1+.
\n⎕NET 'LoadAssembly' 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'\nThe call returns a namespace reference if the assembly loads successfully; any error will be signaled as an APL exception, which you can trap with ⎕EXCEPTION.
\nWorking with .NET types
\nOnce loaded, the assembly’s types appear under the ⎕NET root. For example, System.Windows.Forms.Form becomes accessible as ⎕NET.System.Windows.Forms.Form. You instantiate objects with ⎕NET.New and read/write properties using normal APL assignment.
Worked example: displaying a simple form
\nThe following APL snippet creates a Windows Form, adds a Button, and defines an APL callback for the button’s Click event. No output is shown here; you would run the code in a Dyalog session to see the window appear.
\n∇ ShowForm\n frm←⎕NET.New 'System.Windows.Forms.Form'\n frm.Text←'Demo from APL'\n frm.Size←⎕NET.New 'System.Drawing.Size' 300 200\n\n btn←⎕NET.New 'System.Windows.Forms.Button'\n btn.Text←'Click me'\n btn.Location←⎕NET.New 'System.Drawing.Point' 100 70\n btn.Size←⎕NET.New 'System.Drawing.Size' 100 30\n\n ⎕NET.EventHandler btn.Click 'OnClick' ⍝ bind APL callback\n frm.Controls.Add btn\n frm.ShowDialog\n∇\n\n∇ OnClick sender args\n ⎕←'Button clicked at ',⍕⎕NET.GetProperty sender 'Location'\n∇\nKey points:
\n- \n
⎕NET.Newcreates a .NET object and returns an APL reference. \n- Property assignment (
frm.Text←...) automatically marshals APL character vectors to .NET strings. \n - The
⎕NET.EventHandlercall wires a .NET event to an APL function; the callback receives the sender and event‑args objects. \n - When the form is closed with
ShowDialog, control returns to the APL session. \n
Trade‑offs and limitations
\nWhile ⎕NET is convenient for occasional interop, each call crosses the APL‑.NET boundary, which involves data conversion (marshalling). For tight loops that move large arrays back and forth, this overhead can become noticeable. A practical mitigation is to batch data: collect results in APL, send a single .NET call, or perform the heavy computation inside APL using its native primitives.
\nAnother limitation is platform scope: ⎕NET works only on Windows. If you need cross‑platform .NET interop, consider using Dyalog’s ⎕PY or a REST service instead.
\nActionable closing
\nTo verify that your Dyalog installation can load .NET assemblies:
\n- \n
- Start a Dyalog session. \n
- Run the
⎕NET 'LoadAssembly'statement shown above. \n - If no error is signaled, execute the
ShowFormfunction; a window should appear and respond to button clicks. \n - Inspect
⎕NET.Coreto see the loaded assembly list. \n
If you encounter an assembly‑load error, check that the targeted .NET version is installed and that the PublicKeyToken matches the assembly you referenced. Adjust the version number in the LoadAssembly call accordingly.
By following these steps, you can safely integrate .NET libraries into your APL‑based solutions while keeping the development environment unified.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.