Imported type defined multiple times

600 views
Skip to first unread message

Leo Ashkinaziy

unread,
Jan 22, 2014, 5:14:55 PM1/22/14
to cs-s...@googlegroups.com
Hello,

I'm trying to run a hosted script in Mono, and I keep getting an exception about types being defined multiple times:

{interactive}(7,45): error CS0433: The imported type `System.Console' is defined multiple times
{interactive}(7,91): error CS0433: The imported type `System.DateTime' is defined multiple times
Internal(1,1): warning CS1685: The predefined type `System.Object' is defined multiple times. Using definition from `mscorlib.dll'
Internal(1,1): warning CS1685: The predefined type `System.ValueType' is defined multiple times. Using definition from `mscorlib.dll'
Internal(1,1): warning CS1685: The predefined type `System.Attribute' is defined multiple times. Using definition from `mscorlib.dll'
Internal(1,1): warning CS1685: The predefined type `System.Int32' is defined multiple times. Using definition from `mscorlib.dll'
Internal(1,1): warning CS1685: The predefined type `System.UInt32' is defined multiple times. Using definition from `mscorlib.dll'
Internal(1,1): warning CS1685: The predefined type `System.Int64' is defined multiple times. Using definition from `mscorlib.dll'

The code:

using CSScriptLibrary;

namespace CSScriptTest
{
    public interface IScript
    {
        void Execute();
    }

    public class ScriptTest
    {
        public void RunScript()
        {
            string code = @"
                    using System;
                    using CSScriptTest;

                    public class Script : IScript
                    {                
                        public void Execute()
                        {
                            Console.WriteLine(""Time is {0}"", DateTime.Now.ToString());
                        }
                    }";

            IScript script = CSScript.Evaluator.LoadCode<IScript>(code);
            script.Execute();
        }
    }
}


Any hints where to look? Thanks in advance.

Oleg Shilo

unread,
Jan 22, 2014, 6:20:53 PM1/22/14
to cs-s...@googlegroups.com
Because you are using Evaluator the whole control over the compilation of the script code is entirely up to the Mono compiler-as-service. CS-Script only references with the Mono.CSharp.Evaluator.ReferenceAssembly() and passes the script code to Mono.CSharp.Evaluator.Compile(). It may also decorate the classless script with the class declaration (not your case).

Thus the only way you can change the way your code executes (BTW it runs fine on .NET) is to reference assemblies by yourself. By default CS-Script references all already loaded assemblies from the current AppDomain. And this is where the assembly confict is happening. I suggest you remove all referenced assemblies with reset and add them manually:

CSScript.Evaluator.Reset(false);
CSScript.Evaluator.ReferenceAssemblyOf<DateTime>()
                  .ReferenceAssemblyByName("some assembly name")

                   .....; 
IScript script = CSScript.Evaluator.LoadCode<IScript>(code);
script.Execute();

//you can reference asms by path, by Assembly object, by Type implemented in the assembly, by object of a type implemented in the assembly. 

  
You can also try to use CodeDom instead. Practically the same code:

IScript script = (IScript)CSScript.LoadCode(code).CreateObject("*");
script.Execute(); 

Oleg


--
You received this message because you are subscribed to the Google Groups "CS-Script" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cs-script+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Leo Ashkinaziy

unread,
Jan 22, 2014, 7:02:06 PM1/22/14
to cs-s...@googlegroups.com, osh...@gmail.com
Using Reset() and referencing all the assemblies doesn't work for me. I get the same exception even when running under .NET.

The full code:
public void RunScript()
        {
            string code = @"<snip>";

            CSScript.Evaluator.Reset(false);
            CSScript.Evaluator.ReferenceAssemblyOf<DateTime>();
            CSScript.Evaluator.ReferenceAssemblyOf<ScriptTest>();

            IScript script = CSScript.Evaluator.LoadCode<IScript>(code);

            script.Execute();
        }

This does work using CodeDom, which I can use. One issue I ran into is I get an exception when there is a compiler warning. Is there a way to not throw an exception on warnings?

Thanks again.

--
Leo

Leo Ashkinaziy

unread,
Jan 22, 2014, 7:05:11 PM1/22/14
to cs-s...@googlegroups.com, osh...@gmail.com
I also tried setting CSScript.GlobalSettings.HideCompilerWarnings = true, but still get an exception.

--
Leo

Oleg Shilo

unread,
Jan 22, 2014, 8:31:52 PM1/22/14
to cs-s...@googlegroups.com
CSScript.GlobalSettings.HideCompilerWarnings controls if warnings should be removed from the compile error info. But they do not control if the warnings should be treated as error. It is entirely up to the compiler default settings (csc.exe on Windows and Mono on Linux).

On Windows you can overwrite the defaults by passing the compiler options either from the script or from the host code:

string assembly = CSScript.CompileWithConfig(script, nullfalseCSScript.GlobalSettings, "/warnaserror-");
...
@"//css_co /warnaserror-;
  using System;
  ...
For the list of compiler options see csc.exe help (csc /?):
Inline image 1

And on Linux/Mono it can be something different.

And when you use Evaluator CS-Script has no control over compilation at all. Yes you can set CSScript.Evaluator.WarningsAsErrors to false (the default already) but it only means "do not stop execution if there are warnings". In your case the compiler (Mono.CSharp compiler-as-service) decided to treat warning as error and if you want toi change it then you need to change the Mono compiler settings. Either globally on your system or from the host application CSScript.Evaluator.CompilerSettings which is of the of the Mono.CSharp.CompilerSettings type. 

BTW your code works without any changes stright away on .NET. I tested it in Notepad++ and In VS2013. I have prepared the sample for you: https://dl.dropboxusercontent.com/u/2192462/Support/Leo%20Ashkinaziy/Leo%20Ashkinaziy.7z

Inline image 2

Cheers,
Oleg

image.png
image.png
Reply all
Reply to author
Forward
0 new messages