none
CSharpScript can't get basic example script to work RRS feed

  • Question

  • var scriptText=@"using System;
    using System.Windows.Forms; 
    class Script

        static void Main()
        {
            MessageBox.Show( "Hello World!"); 
        }
    }";

    var scriptOptions = ScriptOptions.Default;

    var mdr = new MetadataReference[] {                    MetadataReference.CreateFromFile(this.GetType().Assembly.Location),
    MetadataReference.CreateFromFile(TypeExtensions.GetSystemAssemblyPathByName("System.Windows.Forms.dll")),
    MetadataReference.CreateFromFile(TypeExtensions.GetSystemAssemblyPathByName("System.dll"))};

    scriptOptions.AddReferences(mdr);

    var script=CSharpScript.Create(scriptText, scriptOptions);

    script.RunAsync();

    Exception:  error CS0234: The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)

    I have tried scriptOptions.AddReference("System.Windows.Forms.dll") to no avail.

    I have also tried css_reference System.Windows.Forms.dll;

    In the debugger I can see that the metadataReferences are set directly to the correct paths for the system dlls, however it does not recognize the System.Windows namespace.





    • Edited by gnimmell Monday, March 4, 2019 9:41 PM
    Monday, March 4, 2019 9:32 PM

Answers

  • Hi gnimmell,

    I test the code again. And I get the solution.

    Here is a simple code your reference. Please do not forget to install Microsoft.CodeAnalysis.Scripting from NuGet.

    var scriptOptions = ScriptOptions.Default.AddReferences(typeof(MessageBox).Assembly).AddImports("System.Windows.Forms");
    
                var Script = @"MessageBox.Show( ""Hello World!"");";
                var result = CSharpScript.EvaluateAsync(Script, scriptOptions).Result;
                Console.ReadLine();

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gnimmell Tuesday, March 5, 2019 7:22 PM
    Tuesday, March 5, 2019 7:46 AM
    Moderator

All replies

  • Hi gnimmell,

    Thank you for posting here.

    For your question, I test the code. I do not reproduce the error. Please provide more details. Where does the "Windows" thrown the error?

    Based on my test, there is a complier error. I fixed it. Please refer to the code.

      var scriptText = @"using System;
    using System.Windows.Forms; 
    class Script
    { 
        static void Main()
        {
            MessageBox.Show( ""Hello World!""); 
        }
        }";
                var scriptOptions = ScriptOptions.Default;
                var mdr = new MetadataReference[]
                {      MetadataReference.CreateFromFile(this.GetType().Assembly.Location),
                       MetadataReference.CreateFromFile(TypeExtensions.GetSystemAssemblyPathByName("System.Windows.Forms.dll")),
                       MetadataReference.CreateFromFile(TypeExtensions.GetSystemAssemblyPathByName("System.dll"))};
    
                scriptOptions.AddReferences(mdr);
    
                var script = CSharpScript.Create(scriptText, scriptOptions);
    
                script.RunAsync();

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 5, 2019 6:06 AM
    Moderator
  • Hi gnimmell,

    I test the code again. And I get the solution.

    Here is a simple code your reference. Please do not forget to install Microsoft.CodeAnalysis.Scripting from NuGet.

    var scriptOptions = ScriptOptions.Default.AddReferences(typeof(MessageBox).Assembly).AddImports("System.Windows.Forms");
    
                var Script = @"MessageBox.Show( ""Hello World!"");";
                var result = CSharpScript.EvaluateAsync(Script, scriptOptions).Result;
                Console.ReadLine();

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gnimmell Tuesday, March 5, 2019 7:22 PM
    Tuesday, March 5, 2019 7:46 AM
    Moderator
  • Thank you Wendy.

    I finally figured out that 

       scriptOptions.AddReferences(mdr);

    Should be...

       scriptOptions = scriptOptions.AddReferences(mdr);

    To get my desired effect I needed to add to the bottom of the script after the class definition. 

       Script.Main();

    • Edited by gnimmell Tuesday, March 5, 2019 7:58 PM
    Tuesday, March 5, 2019 7:22 PM