在 CSharpCompilation .net Core 中没有得到 namespace/reference
Not getting a namespace/reference in CSharpCompilation .net Core
我有一个使用一些动态编译代码的项目,我正在从 .net Framework 升级到 .net core 3.1。
我无法获得包含 newtonsoft.json.dll 的简单测试用例,并收到错误“找不到类型或名称空间名称 'Newtonsoft'。我第一次尝试添加时遇到了类似的问题库,但通过使用当前加载的程序集()通过了它。使用“核心”我没有得到关于库的错误,但它不知道类型,就像它没有得到已加载。
我试过使用项目库(注释掉)和直接指定它们,但有同样的问题。要重新创建,请创建一个名为“TestScript”的新 .netCore 3.1 控制台应用程序并安装 nuget 包“Microsoft.CodeAnalysis.CSharp.Scripting”v 3.7.0、“Newtonsoft.Json”v12.0.3 并使用以下代码。
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
namespace TestScript
{
class Program
{
public static void Example1()
{
var assemblyName = "UserScript";
var code = @"namespace UserScript
{
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public class RunScript
{
private const int x = 99;
public int Eval()
{
JObject j = new JObject();
return x;
}
}
}";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
var references = new List<MetadataReference>();
//Load project libraries
//var assemblies = AppDomain.CurrentDomain
// .GetAssemblies()
// .Where(a => !a.IsDynamic)
// .Select(a => a.Location);
//foreach (var item in assemblies)
//{
// if (!item.Contains("xunit"))
// references.Add(MetadataReference.CreateFromFile(item));
//}
//or specify the libraries to load.
var coreDir = Directory.GetParent(typeof(Enumerable).GetTypeInfo().Assembly.Location);
var exeDir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
references.Add(MetadataReference.CreateFromFile(typeof(Object).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "mscorlib.dll"));
references.Add(MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "System.Runtime.dll"));
if (File.Exists(exeDir + "\Newtonsoft.Json.dll"))
references.Add(MetadataReference.CreateFromFile(exeDir + "\Newtonsoft.Json.dll"));
else
throw new Exception("Missing newtonsoft DLL");
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
},
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
if (result.Success)
{
memoryStream.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(memoryStream.ToArray());
Type testClassType = assembly.GetType("TestNamespace.TestClass");
var addResult = (int)testClassType.GetMethod("Add").Invoke(null, new object[] { 3, 4 });
Console.WriteLine(addResult);
}
else
{
Console.WriteLine("Failed to compile");
for (var i = 0; i < result.Diagnostics.Length; i++)
{
Console.WriteLine(result.Diagnostics[i].ToString());
}
}
}
}
static void Main(string[] args)
{
JObject j = null; //to make sure newtonsoft is included if loading current projects libraries
Example1();
}
}
}
如果您没有忘记使用您建立的 references
列表,您的代码应该可以正常工作。
查看 .NET 上的测试代码 Fiddle (link) - 我在那里使用了 AppDomain
方法(你注释掉的那个)。
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
references, //<-- you're missing this
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
此外,您的调用实际上并没有调用正确的 Class 和方法,我想您会在解决编译问题后发现这一点。
// Non-existent Type and Method...
Type testClassType = assembly.GetType("TestNamespace.TestClass");
var addResult = (int)testClassType.GetMethod("Add").Invoke(null, new object[] { 3, 4 });
编辑:这是完整的工作代码,以防 Fiddle 被删除:
public static void Main(string[] args)
{
var j = new JObject(); // ensure assembly is available
Example1();
}
public static void Example1()
{
var assemblyName = "UserScript";
var code = @"namespace UserScript {
using Newtonsoft.Json;
public class RunScript {
public static string Eval() {
return JsonConvert.SerializeObject(new int[] {1,2,3,4});
}
}
}";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
//Load project libraries
var references = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => a.Location)
.Where(s => !string.IsNullOrEmpty(s))
.Where(s => !s.Contains("xunit"))
.Select(s => MetadataReference.CreateFromFile(s))
.ToList()
;
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
references, //<-- you're missing this
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
if (result.Success)
{
memoryStream.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(memoryStream.ToArray());
var testClassType = assembly.GetType("UserScript.RunScript");
var invokeResult = (string)testClassType.GetMethod("Eval").Invoke(null, null);
Console.WriteLine(invokeResult);
}
else
{
Console.WriteLine("Failed to compile");
foreach (var diag in result.Diagnostics)
Console.WriteLine(diag);
}
}
}
我有一个使用一些动态编译代码的项目,我正在从 .net Framework 升级到 .net core 3.1。
我无法获得包含 newtonsoft.json.dll 的简单测试用例,并收到错误“找不到类型或名称空间名称 'Newtonsoft'。我第一次尝试添加时遇到了类似的问题库,但通过使用当前加载的程序集(
我试过使用项目库(注释掉)和直接指定它们,但有同样的问题。要重新创建,请创建一个名为“TestScript”的新 .netCore 3.1 控制台应用程序并安装 nuget 包“Microsoft.CodeAnalysis.CSharp.Scripting”v 3.7.0、“Newtonsoft.Json”v12.0.3 并使用以下代码。
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
namespace TestScript
{
class Program
{
public static void Example1()
{
var assemblyName = "UserScript";
var code = @"namespace UserScript
{
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public class RunScript
{
private const int x = 99;
public int Eval()
{
JObject j = new JObject();
return x;
}
}
}";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
var references = new List<MetadataReference>();
//Load project libraries
//var assemblies = AppDomain.CurrentDomain
// .GetAssemblies()
// .Where(a => !a.IsDynamic)
// .Select(a => a.Location);
//foreach (var item in assemblies)
//{
// if (!item.Contains("xunit"))
// references.Add(MetadataReference.CreateFromFile(item));
//}
//or specify the libraries to load.
var coreDir = Directory.GetParent(typeof(Enumerable).GetTypeInfo().Assembly.Location);
var exeDir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
references.Add(MetadataReference.CreateFromFile(typeof(Object).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "mscorlib.dll"));
references.Add(MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "System.Runtime.dll"));
if (File.Exists(exeDir + "\Newtonsoft.Json.dll"))
references.Add(MetadataReference.CreateFromFile(exeDir + "\Newtonsoft.Json.dll"));
else
throw new Exception("Missing newtonsoft DLL");
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
},
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
if (result.Success)
{
memoryStream.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(memoryStream.ToArray());
Type testClassType = assembly.GetType("TestNamespace.TestClass");
var addResult = (int)testClassType.GetMethod("Add").Invoke(null, new object[] { 3, 4 });
Console.WriteLine(addResult);
}
else
{
Console.WriteLine("Failed to compile");
for (var i = 0; i < result.Diagnostics.Length; i++)
{
Console.WriteLine(result.Diagnostics[i].ToString());
}
}
}
}
static void Main(string[] args)
{
JObject j = null; //to make sure newtonsoft is included if loading current projects libraries
Example1();
}
}
}
如果您没有忘记使用您建立的 references
列表,您的代码应该可以正常工作。
查看 .NET 上的测试代码 Fiddle (link) - 我在那里使用了 AppDomain
方法(你注释掉的那个)。
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
references, //<-- you're missing this
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
此外,您的调用实际上并没有调用正确的 Class 和方法,我想您会在解决编译问题后发现这一点。
// Non-existent Type and Method...
Type testClassType = assembly.GetType("TestNamespace.TestClass");
var addResult = (int)testClassType.GetMethod("Add").Invoke(null, new object[] { 3, 4 });
编辑:这是完整的工作代码,以防 Fiddle 被删除:
public static void Main(string[] args)
{
var j = new JObject(); // ensure assembly is available
Example1();
}
public static void Example1()
{
var assemblyName = "UserScript";
var code = @"namespace UserScript {
using Newtonsoft.Json;
public class RunScript {
public static string Eval() {
return JsonConvert.SerializeObject(new int[] {1,2,3,4});
}
}
}";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
//Load project libraries
var references = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => a.Location)
.Where(s => !string.IsNullOrEmpty(s))
.Where(s => !s.Contains("xunit"))
.Select(s => MetadataReference.CreateFromFile(s))
.ToList()
;
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
references, //<-- you're missing this
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
if (result.Success)
{
memoryStream.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(memoryStream.ToArray());
var testClassType = assembly.GetType("UserScript.RunScript");
var invokeResult = (string)testClassType.GetMethod("Eval").Invoke(null, null);
Console.WriteLine(invokeResult);
}
else
{
Console.WriteLine("Failed to compile");
foreach (var diag in result.Diagnostics)
Console.WriteLine(diag);
}
}
}