提交 5d4dc5dc 编写于 作者: N Neal Gafter

A nameof() expression is always legal in an expression tree, as it is a constant

Fixes #3923
上级 af126543
......@@ -529,6 +529,13 @@ private BoundNode VisitMethodGroup(BoundMethodGroup node, bool parentIsConversio
return base.VisitMethodGroup(node);
}
public override BoundNode VisitNameOfOperator(BoundNameOfOperator node)
{
// The nameof(...) operator collapses to a constant in an expression tree,
// so it does not matter what is recursively within it.
return node;
}
public override BoundNode VisitNullCoalescingOperator(BoundNullCoalescingOperator node)
{
if (_inExpressionLambda && node.LeftOperand.ConstantValue != null && node.LeftOperand.ConstantValue.IsNull)
......
......@@ -5041,6 +5041,31 @@ class C
}
}
[WorkItem(3923, "https://github.com/dotnet/roslyn/issues/3923")]
[Fact]
public void NameofInExpressionTree()
{
string program = @"
using System;
using System.Linq.Expressions;
public class Program
{
public static void Main()
{
Expression<Func<string>> func = () => nameof(Main);
Console.WriteLine(func.Compile().Invoke());
}
}
";
CompileAndVerify(
sources: new string[] { program },
additionalRefs: new[] { SystemCoreRef },
expectedOutput: @"Main")
.VerifyDiagnostics();
}
#endregion Regression Tests
#region helpers
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册