VS2015 design fails with no error message with dynamic

I wrote unit test on a piece of code that returned JSON. The type that it returns is an anonymous type, so I decided to check the values ​​on it. I just applied the object to dynamic to fulfill my statements.

However, when I do this, my build fails, but I have no 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); } 

Below is a screenshot of a build error

build failing

The line completes successfully when I comment on this statement:

build successing without assert

In contrast, I ran the following code in the beta version of LinqPad 5 (which uses the Roslyn compiler) and had no problems:

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

True

What's going on here? Since the error does not appear, I cannot say if I am using dynamic incorrectly, or if it cannot find the overload used for IsTrue() due to dynamic , or if it is an error in the compiler (although I really doubt it, I have there is no evidence that something is wrong with my code).

Regarding the overload problem, I tried Assert.IsTrue((bool)asDynamic.someValue); but the assembly still fails but no error message appears.

In @RonBeyer's comment, I also tried more casting, like below, to no avail:

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

Upon closer inspection, I found that an error occurred in the output window:

c: ... \ DynamicBuildFailTest \ UnitTest1.cs (16,33,16,42): error CS0656: missing compiler, required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

Well, VS2013 is better to report errors, I will search based on these:

enter image description here

Well, adding a link to Microsoft.CSharp fixed the build error , but I will leave this question open, because this is apparently a problem with VS2015, which (in my opinion) should be resolved.

+62
c # visual-studio-2015 roslyn
Aug 12 '15 at 15:31
source share
5 answers

There is a compiler error, Visual Studio 2015 simply does not report an error. However, Visual Studio 2013 does:

The answer to this is: https://stackoverflow.com/a/212977/

In short:

Add a link to Microsoft.CSharp to use dynamic like this.

+126
Aug 12 '15 at 15:56
source share

As two people noted in the comments, for Net Core and NetStandard this problem is sometimes fixed by adding a NuGet link to Microsoft.CSharp .

+2
Nov 16 '17 at 1:23
source share

There is a known issue with build errors that do not appear in the error list. See, for example, https://github.com/dotnet/roslyn/issues/4567 .

To work around this, in the Error List window, select the drop-down menu to the right of Messages and select Build + IntelliSense.

0
Aug 26 '15 at 4:24
source share

I had a similar problem, and the only thing that resolved it was to update the NUnit package to the latest version.

By the way, when you open the Nuget window, make sure that you do not reduce your package (when I had version 2.0.11, I was shown that I am upgrading to version 2.0.9, which actually lowers the rating ...)

0
Jul 31 '16 at 8:21
source share

If this problem was used using a dynamic keyword in combination with Newtonsoft.json in a .net 3.0 project.

The fix was to completely drop the dynamics and use JObject instead:

from

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

to

 JObject locales = JObject.Parse(this.Locales); 
0
Apr 04 '17 at 11:59 on
source share



All Articles