无法在CSharpCodeProvider脚本中包含Newtonsoft JSON [英] Can't include Newtonsoft JSON in CSharpCodeProvider script
问题描述
我正在尝试在动态编译的脚本中使用JSON.但是,当我包含该库并添加JObject时,会出现类似以下错误:在未引用的程序集中定义了类型'System.Dynamic.IDynamicMetaObjectProvider'.您必须添加对程序集'System.Core,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用."
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'."
我包括MathNet.数值很好.
I included MathNet.Numerics just fine.
这是我的设置.
- 控制台应用程序.NET Framework 4(因此它与运行时匹配 编译)
- Nuget安装MathNet.Numerics
- Nuget安装Newtonsoft
- 将运行时编译文件与dll指向调试文件夹.
- 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.
测试代码
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();
}
}
}
推荐答案
您需要添加对GAC(全局程序集缓存)中的 System 和 System.Core 的引用:
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");
我认为这取决于 Newtonsoft.Json .
这篇关于无法在CSharpCodeProvider脚本中包含Newtonsoft JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!