Luca Morelli Luca Morelli - 1 year ago 197
C# Question

cannot use dynamic type with asp.net vnext core

I need to read and save a JSON file inside an ASP.NETt vNext app, and I would like to use a

dynamic
variable to store the value loaded using JSON.net, but when I go to compile I received this error message:


ASP.NET Core 5.0 error CS1980: Cannot define a class or member that utilizes
'dynamic'
because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute'
cannot be found. Are you missing a reference?


How can solve this? If I use
dynamic
, can I run an application using ASP.NET core?

Answer Source

You need to include the packages Microsoft.CSharp and System.Dynamic.Runtime for the aspnetcore50 framework.

This seems to work for me with CoreCLR version 1.0.0-beta1:

´╗┐using Newtonsoft.Json.Linq;
using System;

namespace DynamicTest
{
    public class Program
    {
        public void Main(string[] args)
        {

            dynamic dobject = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

            Console.WriteLine(dobject.number);
            Console.WriteLine(dobject.str);
            Console.WriteLine(dobject.array.Count);
            Console.ReadLine();
        }
    }
}

The project.json

´╗┐{
    "version": "1.0.0-*",
    "dependencies": {
        "Newtonsoft.Json" :  "6.0.7"
    },
    "commands": { 
        "run" : "run"
    },
    "frameworks" : {
        "aspnet50" : { },
        "aspnetcore50" : { 
            "dependencies": {
                "System.Console": "4.0.0-beta-22231",
                "System.Dynamic.Runtime": "4.0.0-beta-22231",
                "Microsoft.CSharp": "4.0.0-beta-22231"
            }
        }
    }
}