dotnet/roslyn

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

开放

#7,758 创建于 2016年1月3日

 (3 条评论) (0 个反应) (0 位负责人)C# (4,257 个派生)batch import
Area-InteractiveInteractive-ScriptingLogichelp wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南