[Mono-list] Using CSharpCodeProvider in Mono

Paulo Aboim Pinto esqueleto at tusofona.com
Fri Aug 22 07:46:15 EDT 2008


Hello,

I'm developing an application that will run in a Windows box in Linux box
and we have a method to Evaluating Expressions and for that I'm using the
CSharpCodeProvider to create a dynamic method to return the  value of the
math expression.

In windows, this works fine but in linux I got the message:

Unhandled Exception: System.ArgumentException: Expression cannot be
evaluated, please use a valid C# expression: 0+1 (  at
System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String
name, System.String fileName, Boolean emitSymbolInfo, Boolean transient)
[0x00000] )
  at RefletionExample.Program.EvaluateExpression (System.String statement)
[0x00000]
  at RefletionExample.Program.Main (System.String[] args) [0x00000]


The code is very simple:

        static void Main(string[] args)
        {
            Console.WriteLine(EvaluateExpression("0+1"));
        }

        private static string EvaluateExpression(string statement)
        {
            CSharpCodeProvider codeProvider = new CSharpCodeProvider();

            CompilerParameters cp = new CompilerParameters();
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = true;
            cp.OutputAssembly = "Module";

            string ModuleSource = "namespace ns { " +
                                  "using System; " +
                                  "class class1 { " +
                                  "public static string Evaluate(){return ("
+ statement + ").ToString();}}} ";

            CompilerResults cr = codeProvider.CompileAssemblyFromSource(cp,
ModuleSource);

            if (cr.Errors.Count > 0)
            {
                throw new ArgumentException(string.Format("Expression cannot
be evaluated, please use a valid C# expression: {0} ({1})", statement,
cr.Errors[0].ErrorText));
                //return null;
            }
            else
            {
                MethodInfo methodinfo =
cr.CompiledAssembly.GetType("ns.class1").GetMethod("Evaluate");
                return (string)methodinfo.Invoke(null, null);
            }
        }


Can i use this on Mono? What should be the way to do this on Mono!??
The mono version is:
$ mono --version
Mono JIT compiler version 1.9.1 (tarball)
Copyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        GC:            Included Boehm (with typed GC)
        SIGSEGV:       normal
        Notifications: epoll
        Architecture:  x86
        Disabled:      none



Hope you guys help me, in my company was too hard to put something work on
mono and have this problem will kill all mono development... hope there is
an solution for this.


regards
Paulo Aboim Pinto
Odivelas - Portugal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080822/f74e773b/attachment.html 


More information about the Mono-list mailing list