[Mono-bugs] [Bug 420042] New: Using CSharpCodeProvider in Mono

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon Aug 25 10:05:29 EDT 2008


https://bugzilla.novell.com/show_bug.cgi?id=420042


           Summary: Using CSharpCodeProvider in Mono
           Product: Mono: Compilers
           Version: 1.9
          Platform: i686
        OS/Version: Other
            Status: NEW
          Severity: Critical
          Priority: P5 - None
         Component: C#
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: esqueleto at tusofona.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: DeveloperNet


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


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list