c# – VS2015构建失败,没有动态的错误消息
问题:
我正在编写一个返回JSON的代码的单元测试。 它返回的类型是匿名类型,所以我想验证它上面的值我只是将对象转换为dynamic
来做我的断言。
但是,当我这样做时,我的构建失败但我没有任何错误消息。 我能够在新的单元测试项目中使用非常简单的代码重现这一点:
[TestMethod]
public void TestMethod1()
{
var obj = new { someValue = true };
dynamic asDynamic = obj;
Assert.IsTrue(asDynamic.someValue);
}
请参阅下面的构建失败的屏幕截图
当我注释掉断言时,构建成功:
相比之下,我在LinqPad 5 beta(使用Roslyn编译器)中运行了以下代码并且没有任何问题:
var obj = new { someValue = true };
dynamic asDynamic = obj;
Console.WriteLine((asDynamic.someValue == true).ToString());
真正
这里发生了什么? 由于错误没有显示我不能说,如果我使用的是dynamic
,不正确的,或者如果它不能找到超载用于IsTrue()
的,因为dynamic
,或者如果这是在编译器中的错误(虽然我非常怀疑这一点,但我没有任何证据证明我的代码有问题)。
关于过载问题,我尝试了Assert.IsTrue((bool)asDynamic.someValue);
但构建仍然失败,仍然没有错误消息。
根据Per @ RonBeyer的评论,我也尝试了更多的演员,如下所示无济于事:
dynamic asDynamic = (dynamic)obj;
Assert.IsTrue(((dynamic)asDynamic).someValue);
Assert.IsTrue((bool)asDynamic.somevalue);
经过仔细检查,我发现输出窗口中列出了一个错误:
c:… \\ DynamicBuildFailTest \\ UnitTest1.cs(16,33,16,42):错误CS0656:缺少编译器所需的成员’Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create’
好的,VS2013更善于报告错误,我会根据这些搜索:
好的, 添加对Microsoft.CSharp的引用修复了构建错误 ,但我会将此问题保持打开状态,因为它可能是VS2015的问题(在我看来)应该解决。
I was writing a unit test on a piece of code that returned JSON.The type that it returns is an anonymous type, so I thought to verify the values on it I’d just cast the object to a dynamic
to do my assertions.However, when I do that, my build fails but I don’t have any error messages.I was able to reproduce this with very simple code in a new Unit Test Project:See below for a screenshot of the build failingThe build succeeds when I comment out the assertion though:In contrast, I ran the following code in LinqPad 5 beta (which uses the Roslyn compiler) and had no issues:TrueWhat’s going on here?Since the error isn’t showing I can’t tell if I’m using dynamic
incorrectly, or if it can’t find the overload to use for IsTrue()
because of the dynamic
, or if this is a bug in the compiler (though I highly doubt this, I don’t have any evidence that there’s something wrong with my code).Regarding the overload issue, I tried Assert.IsTrue((bool)asDynamic.someValue);
but the build still fails, still no error message.Per @RonBeyer’s comment, I had also tried more casting such as below to no avail:Upon closer inspection, I found that there was an error listed in the Output window:c:…\\DynamicBuildFailTest\\UnitTest1.cs(16,33,16,42): error CS0656: Missing compiler required member ‘Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create’Okay, VS2013 is better at reporting the errors, I will search based on those:Okay, adding a reference to Microsoft.CSharp fixed the build error , but I will leave this question open because it’s presumably an issue with VS2015 that (in my mind) should be resolved.