简体   繁体   中英

when I compile with csc in Linux,the function JArray.Parse in Newtonsoft.Json with error CS0012

This is the case: I compiled C# , using csc in Linux. I used Newtonsoft.Json.Linq.JArray in Newtonsoft.Json, and tested the use of JArray.Parse(string). This error was reported during compilation: error CS0012: The type 'Object' is defined in an assembly that is not referenced.

I tried many ways, but none of them worked... How can I solve this problem? Would anyone like to help me, please?

Here is my test code

Newtonsoft.Json, Version=13.0.0.0, Culture=neutral [TestCode]:

using System;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace test
{
    class JsonTest
    {
        static void Main(string[] args) {
            JArray s = JArray.Parse("[" + "\"test!\"" + "]");
            Console.WriteLine(s[0]);
        }
    }
}

[And csc test.cs]:

> csc -version
3.6.0-4.20224.5 (ec77c100)
> csc -r:Newtonsoft.Json.dll  test.cs
Microsoft (R) Visual C# Compiler version 3.6.0-4.20224.5 (ec77c100)
Copyright (C) Microsoft Corporation. All rights reserved.

test.cs(12,31): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(12,24): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(13,31): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(13,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(14,31): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(14,36): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(15,31): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
test.cs(15,31): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference
to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

===============================================================

I tried to introduce netstandard and it compiled, and success!

> csc -r:/home/Newtonsoft.Json.13.0.1/lib/netstandard2.0/Newtonsoft.Json.dll -r:/usr/lib/mono/4.5/Facades/netstandard.dll test.cs
Microsoft (R) Visual C# Compiler version 3.6.0-4.20224.5 (ec77c100)
Copyright (C) Microsoft Corporation. All rights reserved.

> mono test.exe
test

The version of Newtonsoft.Json you're using is based on netstandard. netstandard is a framework library that allows libraries, such as newtonsoft to be used with different .NET runtimes.

csc requires you to manually specify a reference to the netstandard library:

csc -r:Newtonsoft.Json.dll -r:netstandard test.cs

Also make sure the Newtonsoft.Json.dll is present in the same directory as the resulting executable.

Use dotnet CLI if possible

It's not recommended to use csc directly. It requires knowledge of the DLLs being used and what framework libraries to reference.

For .NET development on Linux (and also Windows), Microsoft has created the dotnet CLI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM