提交 993d5ff1 编写于 作者: V VSadov

Fixed handling of parameterless struct constructors in expression tree lowering.

  (changeset 1333851)
上级 7ff04e96
......@@ -821,7 +821,9 @@ private BoundExpression VisitObjectCreationExpressionInternal(BoundObjectCreatio
return Constant(node);
}
if ((object)node.Constructor == null || node.Arguments.Length == 0)
if ((object)node.Constructor == null ||
(node.Arguments.Length == 0 && !node.Type.IsStructType()) ||
node.Constructor.IsDefaultValueTypeConstructor())
{
return ExprFactory("New", Bound.Typeof(node.Type));
}
......
......@@ -810,5 +810,30 @@ static void Main()
CompileAndVerifyExperimental(source, expectedOutput: "0");
}
[Fact]
public void InstanceInitializerStructInExprTree()
{
var source = @"
using System;
using System.Linq.Expressions;
class C
{
struct S1
{
public int x = 42;
}
static void Main()
{
Expression<Func<S1>> testExpr = () => new S1();
System.Console.Write(testExpr.Compile()().x);
}
}
";
CompileAndVerifyExperimental(source, additionalRefs: new[] { ExpressionAssemblyRef }, expectedOutput: "42");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册