简体   繁体   中英

Interop between C# and other Languages

I have a system, written in C# which is designed to be given a dll that contains a class implementing a certain interface, and then instantiate that class and invoke certain interface methods and use the responses.

The context is to build an AI Competition where people submit their AIs as C# dlls. And it all works perfectly. Yay!


Now I'd like to start supporting other languages, by the same approach.

Fundamentally users should upload a single file (and the metadata about what kind of file that is), and I then invoke the contents of that file accordingly.

For C#, that's "upload a dll" and "use C# System.Reflection to instantiate it to get a C# object implementing the interface, then just call the relevant methods".

I'm interested in knowing what other languages I can viably support? I could interact with anything that can compile down to an .exe and interact over the command line, or I could set up a Web API based interaction.

But what options are there for things that more directly resembling instantiating an object and interacting directly with that object?

  • It seems that Java can do this via IKVM
  • If I had a text file, or zip of files, that contained JS code, then could I invoke that in a C# environment somehow? (Note that this is running on a server - it's not a web page, so I can't trivially inject that JS into a webpage)
  • Scala? (Note that I know nothing whatsoever about Scala, other than that it is a Functional Language)
  • NodeJS? (Again, all I know is that Node is server-side JavaScript)
  • Others?

I'm very happy for answers to be a links to other SO questions that have solved this problem in specific languages - though I would like you to ensure that it is solving it in my particular context, of the user uploading a file (possibly a zip?) and me invoking that directly in a server-side C# enviroment.

Is finding languages that will run within the CLR definitely the best approach here? There's not much consistency in how they all interface with C#, and you'd be doing extra work to support each language.

An alternative would be to spawn a separate, sandboxed, process, and interact via some well-defined interface (eg some agreed protocol using standard I/O streams, or TCP on a particular port or something). IIRC this is how the Google AI challenges worked in the past. This way, you don't have to specifically implement each language you need to support. People just need to provide an executable that meets your interface. Of course, you then have the different problem of safely sandboxing it, but this is still a generic problem rather than one that has to be solved on a per-language basis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM