Calling Scala from C#

Yesterday, I figured out how to call some C# code from Scala. It is a small leap to then call Scala from C#. The process just gets turned around: instead of creating a C# class library and referencing it from Scala, you create a dll from Scala code and reference it in .Net.

First, I built a class in Scala similar to my C# class from yesterday:

class ScalaSpeaker {

    def Speak() {

        println("Hello");

    }

    

    def Speak(name: String) {

        println("Hi, " + name + ", glad you could join us!")

    }

}

Just like before, the Scala needs to be compiled to MSIL. There are fewer arguments needed because the Scala is not referencing any other dlls and does not contain the entry point for the executable:

<Scala.Net directory>\bin\scalacompiler.exe

–target:msil

–Xassem-extdirs <Scala.Net directory>\bin

<scala files>

Where:

  • <Scala.Net directory> is the directory where the Scala.Net SVN repository was checked out to

After the MSIL has been generated, it can be turned into a dll:

ilasm /DLL <MSIL file>

Notice the /DLL flag for ilasm, this is where the magic happens.

After running ilasm, the new Scala dll can be referenced in a .Net application. I created a console application and added a reference to the dll. For the application to run propertly there are a few more dlls that need to be referenced, all of which can be found in the bin directory of the SVN repository:

CSharpFilesForBootstrap.dll

IKVM.OpenJDK.Core.dll

jfisl.dll

scalalib.dll

scalaruntime.dll

The C# is very straight forward, and surprisingly doesn’t require any extra using statements:

using System;

namespace CallScala

{

    class Program

    {

        static void Main(string[] args)

        {

            var speaker = new ScalaSpeaker();

            speaker.Speak();

            speaker.Speak("Jim");

        }

    }

}

This entry was posted in .Net, C#, Scala. Bookmark the permalink.

3 Responses to Calling Scala from C#

  1. Teo says:

    Hi Ben,
    I have tried your example using the latest version of scalacompiler.exe downloaded from here: https://github.com/downloads/magarciaEPFL/scaladotnet/scala-bin-20120310-ee51b6e1b1.zip
    It appear s the target:msil option is superseded by target:library which creates the DLL for you. However, the resultant DLL cannot be added as a reference in Visual Studio (neither VS2005 nor VS2008) and gives the message:
    *A reference to my.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.*
    Just wondering if you can throw any light on this?
    Thanks

  2. Teo says:

    It requires .NET 4.0 (ildasm manifest shows Metadata version: v4.0.30319) which means VS2010.

    • Ben says:

      Cool, glad you figured this out! It’s been a while since I’ve done any .Net work in Visual Studio and even longer since I’ve looked at Scala.Net.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>