Using your own C# Class Libraries from APLX

This Wiki includes numerous examples of how APLX can be used to call .NET code, but most of the examples use the Microsoft frameworks. What do you do if you have your own class library?

As this article will demonstrate, everything remains the same. The only additional step you need to take care of is making sure that APLX can find your library.

Example C# Code

Let's produce a simple class library which we can use to demonstrate the process. It doesn't do anything which we couldn't do far more simply in APL, but it should serve as an example. Here is the C# source code:

using System;
namespace MyNamespace
{
    public class MyClass
    {
        // Public field
        public String Description = "Simple class library";

        // Private property
        private int _myProp;

        // Constructor
        public MyClass(int arg)
        {
            _myProp = arg;
        }
        

        // Public property getter/setter
        public int Property
        {
            get { return _myProp; }
            set { _myProp = value; }
        }

        // Public method
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

In order to compile this code into a class library, the easiest way is to use Microsoft's Visual Studio (or the free Visual Studio Express). Select "New Project" from the File menu, and then select the "Class Library" template. Replace the boilerplate source code with the version above. Then save the project and build it to produce a .NET DLL.

Adding the class library to the APLX search path

In order to use the new class library you need to tell APLX where it is located using ⎕SETUP. This is typically done once, when APLX launches.

The exact path will vary, but it will be something like:

      ⍝ Get existing path
      s←'.net' ⎕setup 'using'

      ⍝ Add new libary. Note that this string constists of two parts, separated by a comma
      ⍝   (a) The namespace as declared in the C# source code
      ⍝   (b) The path to the DLL
      s←s,⊂'MyNamespace,C:\Users\Simon\Documents\MyClassLibrary\MyClassLibrary\bin\Release\MyClassLibrary.dll'

      ⍝ Update the path
      '.net' ⎕setup (⊂'using'), s

Trying the class library out

Time to check out whether it worked. Let's create an object and try a few of its members:

      ⍝ Create object, providing additional argument 10 to constructor 
      obj←'.net' ⎕new 'MyClass' 10

       ⍝ Check field access
      obj.Description
Simple class library

       ⍝ Check property access
      obj.Property
10
      obj.Property←20
      obj.Property
20

       ⍝ Check method calls
      obj.Add 3 4
7

       ⍝ Use'reflection' API to list object fields, properties, methods and constructors
      obj.⎕desc 2 3 10
Int32 Add(Int32, Int32)
System.String Description
Boolean Equals(System.Object)
Int32 get_Property()
Int32 GetHashCode()
System.Type GetType()
Void MyClass(Int32)
Int32 Property
Void set_Property(Int32)
System.String ToString()




Author: SimonMarsden


CategoryDotNet CategoryAplx

callingyourowncsharpcodeaplx (last edited 2010-03-23 13:29:49 by SimonMarsden)