Reputation: 83
I have a program that takes a .cs script file as input. I want to read from a JSON file in the script and I'm trying to use the JSON.NET library to do it. However, I'm unable to properly reference the dll file to use the functions inside. I'm NOT using Visual Studio to write the script hence I cannot add the reference using the conventional method.
My code:
//css_reference Newtonsoft.Json.dll;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.JsonConvert;
public class Script
{
public async void Action(String path)
{
StreamReader re = new StreamReader("job.json");
JsonTextReader reader = new JsonTextReader(re);
JsonSerializer se = new JsonSerializer();
object parsedData = se.Deserialize(reader);
There were three files associated with Newtonsoft library, the .dll file, a .pdb file and a .Json file and all three are placed in the same folder as the script file.
Error:
Namespace Newtonsoft cannot be found in ...
Namespace Newteonsoft.Json cannot be found in ...
If I remove the three using
statements, I get namespace not found for StreamReader, JsonTextReaader and JsonSerializer.
I'd like help regarding this issue. Thanks!
Upvotes: 0
Views: 2104
Reputation: 11
For me, solution was to use css_nuget at the top of the file, like this:
//css_nuget Newtonsoft.Json;
using Newtonsoft;
using Newtonsoft.Json;
Upvotes: 1
Reputation: 2797
From within your script, you need to load the assembly for JSON.net, using System.Reflection.Assembly.Load(). You should then be able to use reflection to create instances of the JSON.net classes and invoke methods on them. If you combine this with the dynamic runtime, you should be able to instantiate objects and work with them.
Upvotes: 0
Reputation: 11
From what you have posted it looks like you have manually installed JSON.NET, i would recommend installing it via NuGet, that way your project should have the references to the dll's set during the installation process. I have used JSON.NET a number of time in .Net Framework and .Net Core projects and that is how i have always installed it and never had an issue.
Upvotes: 0