43

I'm trying to use a dynamic variable in a C# .net core app that's targeting .net standard 1.6. (platform? library? framework? meta-framework?) I first encountered this problem in a real application, but I have reduced it to a minimal reproduction.

project.json

{
    "version": "1.0.0-*",
    "buildOptions": { "emitEntryPoint": true },
    "dependencies": { "NETStandard.Library": "1.6.0" },
    "frameworks": {
        "netstandard1.6": { "imports": "dnxcore50" }
    },
    "runtimes": { "win10-x64": {} }
}

Program.cs

using System;

public class Program {
    public static void Main(string[] args) {
        dynamic hello = "hello world";
        Console.WriteLine(hello);
    }
}

When I try to build this, I'm getting a build error on Console.WriteLine(hello); saying this.

CS0656 Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

Is it possible to use dynamic variables in an application targeting netstandard 1.6? How?

recursive
  • 83,943
  • 34
  • 151
  • 241

3 Answers3

64

Add System.Dynamic.Runtime and Microsoft.CSharp as dependencies.

Set
  • 47,577
  • 22
  • 132
  • 150
  • 3
    I ended up having to add the following dependencies and versions: "System.Dynamic.Runtime": "4.0.11", "Microsoft.CSharp": "4.0.1", "Microsoft.NETCore.Runtime.CoreCLR": "1.0.4". It's able to build and run now. – recursive Sep 02 '16 at 21:52
  • 7
    `Microsoft.CSharp` depends on `System.Dynamic.Runtime`, so you don't need to add both. – svick Sep 03 '16 at 09:45
  • 3
    @recursive: I concur. I had to add all three of these as well. Maybe it has something to do with the fact that it's a .NET Standard project? – Brent Rittenhouse Nov 21 '17 at 20:40
32

Right-click the project > Manage NuGet Packages... > Add the following two highlighted packages: enter image description here

Hong
  • 17,643
  • 21
  • 81
  • 142
  • This fixed my issue. I am running .net core 2.1 (or 2.2... same issue). I only needed to add Microsoft.CSharp. – ebol2000 May 24 '19 at 20:42
7

If you're writing an application, not a library, you should use Microsoft.NETCore.App, not NETStandard.Library and netcoreapp1.0, not netstandard1.6. Doing that would fix your issue.

If you want to use dynamic in a library (or application that does not depend on Microsoft.NETCore.App), you need to add Microsoft.CSharp as a dependency.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Is there an authoritative reference for which "frameworks" should be used for which purposes? I've been looking for information on what all this means, and all I find are contradictory blogs and similar. What's your source for this information? – recursive Sep 03 '16 at 14:12
  • For a start, that's what the project.json produced by `dotnet new -t console` and `dotnet new -t lib` produce. For more explanation, see [the article *Packages, Metapackages and Frameworks* in .Net docs](https://learn.microsoft.com/en-us/dotnet/articles/core/packages), specifically [the section on Package-based Frameworks](https://learn.microsoft.com/en-us/dotnet/articles/core/packages#package-based-frameworks). – svick Sep 03 '16 at 14:22