Dynamic reference in a .net core app targeting net standard 1.6?

15.1k Views Asked by At

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?

3

There are 3 best solutions below

3
Set On BEST ANSWER

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

2
svick On

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.

1
Hong On

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