dotnet/roslyn

Improve error message when accessing globals within a class in a C# script

Open

#7.758 geöffnet am 3. Jan. 2016

Auf GitHub ansehen
 (3 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C# (4.257 Forks)batch import
Area-InteractiveInteractive-ScriptingLogichelp wanted

Repository-Metriken

Stars
 (20.414 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

Having Globals defined as

public class Globals
{
    public int X;
    public int Y;
}

the following works as expected

Console.WriteLine(CSharpScript.EvaluateAsync<int>(
    "X + Y",
    globals: new Globals { X = 1, Y = 2 }).Result);

while the following

Console.WriteLine(CSharpScript.EvaluateAsync<int>(
    @"class Foo
    {
        public int Bar()
        {
            return X + Y;
        }
    }

    var foo = new Foo();
    foo.Bar()", globals: new Globals { X = 1, Y = 2 }).Result);

fails with exception

Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (5,24): error CS0120: An object reference is required for the non-static field, method, or property 'Globals.Y'

Expected behavior: The second sample executes successfully and returns (int)3.

Contributor Guide