I am running Mono version 2.10 on Ubuntu 11.10. I am trying to run the sample provided on http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.html ,but it seems to target a different version of mono. For instance Compile is a static method on Evaluator. I have made the following changes to his sample, but haven't got it working. Can anyone provide the correct changes, and does anyone know if there is any info on the API changes to Mono.CSharp? The version reported by my compiler is as follows:

$ dmcs --version
Mono C# compiler version 2.10.5.0

I compiled the following code using this command line:

dmcs -r:Mono.CSharp Sample.cs

And got this warning when compiling.

dmcs -r:Mono.CSharp Sample.cs
Sample.cs(26,17): warning CS0219: The variable `compiledMethod' is assigned but its value is never used
Compilation succeeded - 1 warning(s)

This is the result of running the code:

$ ./Sample.exe 
{interactive}(2,40): error CS1525: Unexpected symbol `namespace', expecting `end-of-file' or `using'
{interactive}(4,70): error CS0101: The namespace `UserCode' already contains a definition for `Foo'
{interactive}(4,70): (Location of the symbol related to previous error)

This is the Code I have so far:

using System;
using System.IO;
using Mono.CSharp;
using System.Reflection;

namespace Sample
{
    public interface IFoo { string Bar(string s); }

    class Program
    {
        const string code = @"
            using System;
            namespace UserCode
            {
                public class Foo : Sample.IFoo
                {
                    public string Bar(string s) { return s.ToUpper(); }
                }
            }
        ";

        static void Main(string[] args)
        {
            Mono.CSharp.Evaluator.Init(new string[] {} );
            Evaluator.ReferenceAssembly(Assembly.GetExecutingAssembly());

            var compiledMethod = Evaluator.Compile(code);

            for (;;)
            {
                string line = Console.ReadLine();
                if (line == null) break;

                object result;
                bool result_set;
                Evaluator.Evaluate(line, out result, out result_set);
                if (result_set) Console.WriteLine(result);
            }
        }
    }
}
有帮助吗?

解决方案 2

According to this: http://www.mono-project.com/Release_Notes_Mono_2.12#Instance_API , the static/global Evaluator is the older API and the Instance API is the newer one. The Mono I have is the current stable version (2.10) and Mono.CSharp that comes with version 2.11 has the instance methods. 2.12 doesn't look to have been released yet.

Here's another mention of the instance API to the Compiler as a service: http://tirania.org/blog/archive/2011/Oct-14.html

其他提示

Mono 2.10 comes with Evaluator supporting expressions and statements only. Your code contains type declaration which is not supported by Mono 2.10.

Mono 2.11 or git master Mono.CSharp have support for type declarations and other advanced features.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top