3

I recently use Roslyn to compile and execute code at the runtime of a game application. Thank to some useful ressource such as this web site and Vendettamit answer, I manage to code a program on a C# Console Net Core project on window 10 which execute this code located in a txt file at the root of the application.

using System;

namespace Test15
{
    public class Program
    {
        public static int Main()
        {
            System.Console.WriteLine("Hello World from external Dll !");
            System.Console.WriteLine("And it work !");
            return 10;
        }
    }
}

I'm not going to share code because it is really similar to Vendettamit answer, and this program work well on a C# Console Net Core project on window 10.

So next step, I try to make this program work with the C# Monogame Framework on a Android project which use Xamarin.

First problem : when trying to add the nugget package "Microsoft.CodeAnalysis" which seem necessary for Roslyn, I have the 2 following error :

Unable to resolve reference 'Humanizer', referenced by `Microsoft.CodeAnalysis.CSharp.Workspaces`.
Add NuGet package or assembly reference for 'Humanizer', or remove the reference to
'Microsoft.CodeAnalysis.CSharp.Workspaces'. Game2

Unable to resolve reference 'SQLitePCLRaw.core', referenced by `Microsoft.CodeAnalysis.Workspaces`.
Add NuGet package or assembly reference for 'SQLitePCLRaw.core', or remove the reference to
'Microsoft.CodeAnalysis.Workspaces'. Game2

(Translated from French)

When replacing the "Microsoft.CodeAnalysis" nugget package by "microsoft.CodeAnalysis.CSharp", both errors disappears.

However at the runtime, adding the MetadataReference don't work. For instance in the Console project I use : MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location);

To add a MetadataReference to mscorlib.dll, but on an Android project, it crash because typeof(object).GetTypeInfo().Assembly.Location return "mscorlib.dll" instead of somethings like "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.9\System.Private.CoreLib.dll", because we are on Android I guess. (same with other MetadataReference )

So when calling the method CSharpCompilation.Create(), the "reference" argument is an array of null MetadataReference and it crash.

Based on what I read, I think (but I'm not sure) that Roslyn can't work on android because of the missing location of System.Object.

So can someone can confirm (or invalidate) if there is a way to work with Roslyn on Android ?

(Bonus point if you know some other way to compile/execute or "interpret" C# code on any device)

Thank for reading and stay safe.

Thomas
  • 75
  • 1
  • 9
  • 1
    it seems other people have this problem, too : https://github.com/dotnet/roslyn/issues/23537 - maybe try to ping on github for a solution? – Andrei Epure Nov 05 '20 at 10:49
  • Yes I will try to post the question somewhere else to have a confirmation if this is normal or not. The weird thing is it also crash when I'm using the Scripting functionality (and not the compiling one) for roslyn (so no .dll generated). In all case I'm still searching for a cross platform scripting/compiling language. – Thomas Nov 06 '20 at 21:01
  • 1
    I never saw a followup, did you get roslyn to work with xamarin in the end? – wendellmva Sep 16 '21 at 11:14
  • Nope, I didn't manage to make it work on android (and I don't think that possible), so I switched to interpreted language with bytecode (I'm working on) – Thomas Sep 16 '21 at 16:37

0 Answers0