Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Mono.CSharp interpreter in Microsoft.NET

I was under the impression Mono's compiler was usable in Microsoft.NET

edit: updated blog posting here that I originally missed that explains some of it (is consistent with Justin's answers)

I created a simple class to try to use it

[TestFixture]
class Class1
{
    [Test]
    public void EXPR()
    {
        Evaluator.Run("using System;");

        int sum = (int)Evaluator.Evaluate("1+2");

    }
}

And a project in Visual Studio 2010 that references C:\Program Files (x86)\Mono-2.10.1\lib\mono\4.0\Mono.CSharp.dll.

However when I try to run this task I get the following exception, thrown at the Evaluator.Run call:

    System.TypeInitializationException was unhandled by user code
      Message=The type initializer for 'Mono.CSharp.Evaluator' threw an exception.
      Source=Mono.CSharp
      TypeName=Mono.CSharp.Evaluator
      StackTrace:
           at Mono.CSharp.Evaluator.Run(String statement)
           at Experiments.Class1.EXPR() in W:\Experiments\Class1.cs:line 16
      InnerException: System.TypeLoadException
           Message=Method 'Mono.CSharp.Location.ToString()' is security transparent, but is a member of a security critical type.
           Source=Mono.CSharp
           TypeName=Mono.CSharp.Location.ToString()
           StackTrace:
                at Mono.CSharp.Evaluator..cctor()
           InnerException: 

A google confirms one other person asking this question but no answer. I tried to start reading the microsoft article on security transparent code but got confused quite quickly. Would someone be able to suggest a quick workaround to allow me to use this? And possibly summarise the security implications, if any, to me (in the context of my situation - in the future I hope to package it with a thick client application, to be used both internally and by end-users)

like image 486
fostandy Avatar asked Nov 28 '25 09:11

fostandy


1 Answers

It has worked under .NET since April of last year.

Small point but I notice you are missing a semi-colon in your expression for sum.

int sum = (int)Evaluator.Evaluate("1+2;");

I only have Mono 2.11 (from git) at the moment and they have changed to using a multi-instance version of the compiler instead of the static version. So, my code looks a little different:

using System;
using Mono.CSharp;

namespace REPLtest
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var r = new Report (new ConsoleReportPrinter ());
            var cmd = new CommandLineParser (r);

            var settings = cmd.ParseArguments (args);
            if (settings == null || r.Errors > 0)
                Environment.Exit (1);

            var evaluator = new Evaluator (settings, r);

            evaluator.Run("using System;");

            int sum = (int) evaluator.Evaluate("1+2;");

            Console.WriteLine ("The sum of 1 + 2 is {0}", sum);
        }
    }
}

EDIT: I guess I should confirm that I did in fact successfully execute this on .NET 4 (using Visual C# Express 2010 on Windows XP)

EDIT AGAIN: If you have Visual Studio, you can download the latest version of Mono.CSharp and compile it yourself. There is a .sln (solution file) included with the source so you can build it on Windows without Mono. The resulting assembly would run the code above. Miguel has a post explaining the new Mono.CSharp here.

FINAL EDIT: I uploaded the compiled Mono.CSharp.dll assembly that I actually used here. Include it as a reference to compile the code above.

like image 152
Justin Avatar answered Nov 29 '25 23:11

Justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!