136

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:

[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

build failing

The build succeeds when I comment out the assertion though:

build succeeding without assert

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

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).

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:

    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'

Okay, VS2013 is better at reporting the errors, I will search based on those:

enter image description here

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.

10
  • Are you sure it's actually a compile failure rather than a link failure?
    – David W
    Aug 12, 2015 at 15:36
  • Can you try dynamic asDynamic = (dynamic)obj;? Or just in the Assertion, comment out the dynamic and write Assert.IsTrue(((dynamic)obj).someValue);.
    – Ron Beyer
    Aug 12, 2015 at 15:36
  • @RonBeyer yes I had tried both of those as well, not luck.
    – DLeh
    Aug 12, 2015 at 15:37
  • One more... Assert.IsTrue((bool)asDynamic.someValue);?
    – Ron Beyer
    Aug 12, 2015 at 15:38
  • 1
    I faced the same issue in VS2015 while trying to use dynamic in test methods. The build failed without any errors. And after adding the Microsoft.CSharp reference the build succeeded. Nov 20, 2015 at 7:02

5 Answers 5

227

There is a compiler error, Visual Studio 2015 just does not report the error properly. However, Visual Studio 2013 does:

This is answered here: https://stackoverflow.com/a/13568247:

In short:

Add a reference to Microsoft.CSharp in order to use dynamic like this.

3
  • 9
    Add the reference to Microsoft.CSharp dll even though using Microsoft.CSharp; isn't throwing a compile time error. Jun 16, 2017 at 6:39
  • 45
    With .NET Core add the NuGet package Microsoft.CSharp instead. Aug 9, 2017 at 6:54
  • 6
    The same for .Net Standard based class library - add NuGet package Microsoft.CSharp.
    – Hong
    Feb 16, 2018 at 4:33
50

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

3
  • 3
    This solved my problem after converting a project to .NET Standard, thank you! Dec 11, 2017 at 13:01
  • 1
    Ditto with an SSIS script adding an excel sheet.
    – SteveCav
    Jan 17, 2018 at 0:23
  • @JoakimSkoog ... I had this problem on a .NET Standard project (never converted) and still had to add in a reference manually.
    – ebol2000
    Feb 22, 2018 at 23:57
2

Had this issue using dynamic keyword in combination with Newtonsoft.json in a .net 3.0 project.

The fix was to drop dynamic altogether and use JObject instead:

from

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

to

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

There is a known issue with build errors not appearing in the error list. See, for example, 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".

0

I had a similar issue and the only thing that solved it to me was to upgrade my NUnit package to the latest version.

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...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.