简体   繁体   English

VS2015构建失败,没有动态的错误消息

[英]VS2015 build fails with no error message with Dynamic

I was writing a unit test on a piece of code that returned JSON. 我正在编写一个返回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. 它返回的类型是匿名类型,所以我想验证它上面的值我只是将对象转换为dynamic来做我的断言。

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: 我能够在新的单元测试项目中使用非常简单的代码重现这一点:

[TestMethod]
public void TestMethod1()
{
    var obj = new { someValue = true };

    dynamic asDynamic = obj;

    Assert.IsTrue(asDynamic.someValue);
}

See below for a screenshot of the build failing 请参阅下面的构建失败的屏幕截图

建立失败

The 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: 相比之下,我在LinqPad 5 beta(使用Roslyn编译器)中运行了以下代码并且没有任何问题:

var obj = new { someValue = true };
dynamic asDynamic = obj;
Console.WriteLine((asDynamic.someValue == true).ToString());

True 真正

What'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). 由于错误没有显示我不能说,如果我使用的是dynamic ,不正确的,或者如果它不能找到超载用于IsTrue()的,因为dynamic ,或者如果这是在编译器中的错误(虽然我非常怀疑这一点,但我没有任何证据证明我的代码有问题)。

Regarding the overload issue, I tried Assert.IsTrue((bool)asDynamic.someValue); 关于过载问题,我尝试了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: 根据Per @ RonBeyer的评论,我也尝试了更多的演员,如下所示无济于事:

    dynamic asDynamic = (dynamic)obj;
    Assert.IsTrue(((dynamic)asDynamic).someValue);

    Assert.IsTrue((bool)asDynamic.somevalue);

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' c:... \\ DynamicBuildFailTest \\ UnitTest1.cs(16,33,16,42):错误CS0656:缺少编译器所需的成员'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

Okay, VS2013 is better at reporting the errors, I will search based on those: 好的,VS2013更善于报告错误,我会根据这些搜索:

在此输入图像描述

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. 好的, 添加对Microsoft.CSharp的引用修复了构建错误 ,但我会将此问题保持打开状态,因为它可能是VS2015的问题(在我看来)应该解决。

There is a compiler error, Visual Studio 2015 just does not report the error properly. 有一个编译器错误,Visual Studio 2015只是没有正确报告错误。 However, Visual Studio 2013 does: 但是,Visual Studio 2013可以:

This is answered here: https://stackoverflow.com/a/13568247 : 这在这里得到解答: https//stackoverflow.com/a/13568247

In short: 简而言之:

Add a reference to Microsoft.CSharp in order to use dynamic like this. 添加对Microsoft.CSharp的引用,以便像这样使用dynamic

正如两位人士在评论中指出的那样,对于Net Core和NetStandard,有时可以通过向Microsoft.CSharp添加NuGet引用来解决此问题。

There is a known issue with build errors not appearing in the error list. 存在一个已知问题,即错误列表中没有出现构建错误。 See, for example, https://github.com/dotnet/roslyn/issues/4567 . 例如,请参阅https://github.com/dotnet/roslyn/issues/4567

To work around it, in the "Error List" window, select the pulldown menu to the right of "Messages" and select "Build + IntelliSense". 要解决此问题,请在“错误列表”窗口中,选择“消息”右侧的下拉菜单,然后选择“构建+智能感知”。

I had a similar issue and the only thing that solved it to me was to upgrade my NUnit package to the latest version. 我有一个类似的问题,唯一能解决它的问题是将我的NUnit软件包升级到最新版本。

By the way when you open the Nuget window make sure that your not downgrading your package (when I had version 2.0.11 it showed me to upgrade to version 2.0.9 which is actually downgrading...) 顺便说一下,当你打开Nuget窗口时,请确保你没有降级你的软件包(当我有2.0.11版本时,它显示我升级到版本2.0.9,这实际上是降级...)

Had this issue using dynamic keyword in combination with Newtonsoft.json in a .net 3.0 project. 有这个问题在.net 3.0项目中使用动态关键字与Newtonsoft.json结合使用。

The fix was to drop dynamic altogether and use JObject instead: 修复是完全放弃动态并使用JObject代替:

from

dynamic locales = JObject.Parse(this.Locales);

to

JObject locales = JObject.Parse(this.Locales);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM