问题
I am trying to use JSON in a dynamically compiled script. However, when I include the library and add a JObject, I get errors like: "The type 'System.Dynamic.IDynamicMetaObjectProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."
I included MathNet.Numerics just fine.
Here is my setup.
- Console application .NET Framework 4 (so it matches the runtime compile)
- Nuget install MathNet.Numerics
- Nuget install Newtonsoft
- Point the runtime compile to the debug folder with the dlls.
Test Code
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Microsoft.CSharp;
using System.Reflection;
using System.CodeDom.Compiler;
namespace ConsoleApp1
{
public class RunScript0
{
public bool Compile()
{
//Add using statements here
string source = "namespace UserScript\r\n{\r\nusing System;\r\n" +
"using System.IO;\r\n" +
"using System.Collections.Generic;\r\n" +
"using MathNet.Numerics.Distributions;\r\n" +
"using Newtonsoft.Json.Linq;\r\n" +
"using Newtonsoft.Json;\r\n" +
"public class RunScript\r\n{\r\n" +
"private const int x = 99;\r\n" +
"public int Eval()\r\n{\r\n" +
//Remove following line and all works fine
"JObject j = new JObject();\r\n" +
"return x; \r\n\r\n}\r\n}\r\n}";
Dictionary<string, string> provOptions =
new Dictionary<string, string>();
provOptions.Add("CompilerVersion", "v4.0");
CodeDomProvider compiler = new CSharpCodeProvider(provOptions);
CompilerParameters parameters = new CompilerParameters();
string appPath = System.IO.Directory.GetCurrentDirectory();
parameters.ReferencedAssemblies.Add(appPath + "\\MathNet.Numerics.dll");
parameters.ReferencedAssemblies.Add(appPath + "\\Newtonsoft.Json.dll");
//Tried adding but didn't help parameters.ReferencedAssemblies.Add(appPath + "\\System.Core.dll");
parameters.GenerateInMemory = true;
var results = compiler.CompileAssemblyFromSource(parameters, source);
// Check for compile errors / warnings
if (results.Errors.HasErrors || results.Errors.HasWarnings)
{
Console.WriteLine(results.Errors.Count.ToString() + " Erorrs");
for (int i = 0; i < results.Errors.Count; i++)
Console.WriteLine(results.Errors[i].ToString());
return false;
}
else
{
Console.WriteLine("Compiled Successfully");
return true;
}
}
}
class Program
{
static void Main(string[] args)
{
RunScript0 A = new RunScript0();
A.Compile();
}
}
}
回答1:
You need add a reference to System and System.Core from the GAC (Global Assembly Cache) :
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
I think it's dependances from Newtonsoft.Json.
来源:https://stackoverflow.com/questions/63343467/cant-include-newtonsoft-json-in-csharpcodeprovider-script