gpt4 book ai didi

c# - 运行时编译期间收到 “You must add a reference to assembly ' netstandard'”错误

转载 作者:行者123 更新时间:2023-12-02 10:56:30 25 4
gpt4 key购买 nike

使用C#运行时编译( Roslyn ),我收到许多错误,尽管我在 netcoreapp 的上下文中进行了编译,但我并未添加对 netstandard 的引用。

错误以以下格式显示:

The type 'XXX' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0...



可以将导致此问题的代码处理简化为以下代码段:
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Compiler
{
public static class Compiler
{
public static Assembly CompileAssembly(string code, bool compileAsExecutable = false)
{
var outputKind = compileAsExecutable ? OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary;
var syntaxTree = CSharpSyntaxTree.ParseText(code);
var syntaxTrees = new[] {syntaxTree};
var systemAssemblyReference = GetMetadataReference<object>();
var patchBandAssemblyReference = GetMetadataReference<AssemblyTargetedPatchBandAttribute>();
var references = new[] {systemAssemblyReference, patchBandAssemblyReference};
var cSharpCompilationOptions = new CSharpCompilationOptions(outputKind);
var compilation = CSharpCompilation.Create(@"Countdown", syntaxTrees, references, cSharpCompilationOptions);

using (var assemblyStream = new MemoryStream())
{
var emitResult = compilation.Emit(assemblyStream);

if (emitResult.Success)
{
var assemblyBytes = assemblyStream.ToArray();

return Assembly.Load(assemblyBytes);
}

var errors = emitResult
.Diagnostics
.Select(diagnostic => diagnostic.GetMessage())
.Select(message => new Exception(message));

throw new AggregateException(errors);
}
}

private static string GetAssemblyLocation<T>()
{
var typeOfT = typeof(T);

return typeOfT.Assembly.Location;
}

private static PortableExecutableReference GetMetadataReference<T>()
{
var assemblyLocation = GetAssemblyLocation<T>();

return MetadataReference.CreateFromFile(assemblyLocation);
}
}
}

最佳答案

解决方案是添加对 netstandard.dll 的引用:

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Compiler
{
public static class Compiler
{
public static Assembly CompileAssembly(string code, bool compileAsExecutable = false)
{
var outputKind = compileAsExecutable ? OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary;
var syntaxTree = CSharpSyntaxTree.ParseText(code);
var syntaxTrees = new[] {syntaxTree};

// Requested the reference be fetched
var runtimeSpecificReference = GetRuntimeSpecificReference();
var systemAssemblyReference = GetMetadataReference<object>();
var patchBandAssemblyReference = GetMetadataReference<AssemblyTargetedPatchBandAttribute>();

// Added the reference to the value here
var references = new[] {runtimeSpecificReference, systemAssemblyReference, patchBandAssemblyReference};

var cSharpCompilationOptions = new CSharpCompilationOptions(outputKind);
var compilation = CSharpCompilation.Create(@"Countdown", syntaxTrees, references, cSharpCompilationOptions);

using (var assemblyStream = new MemoryStream())
{
var emitResult = compilation.Emit(assemblyStream);

if (emitResult.Success)
{
var assemblyBytes = assemblyStream.ToArray();

return Assembly.Load(assemblyBytes);
}

var errors = emitResult
.Diagnostics
.Select(diagnostic => diagnostic.GetMessage())
.Select(message => new Exception(message));

throw new AggregateException(errors);
}
}

private static string GetAssemblyLocation<T>()
{
var typeOfT = typeof(T);

return typeOfT.Assembly.Location;
}

private static PortableExecutableReference GetMetadataReference<T>()
{
var assemblyLocation = GetAssemblyLocation<T>();

return MetadataReference.CreateFromFile(assemblyLocation);
}

// This function was needed
private static PortableExecutableReference GetRuntimeSpecificReference()
{
var assemblyLocation = GetAssemblyLocation<object>();
var runtimeDirectory = Path.GetDirectoryName(assemblyLocation);
var libraryPath = Path.Join(runtimeDirectory, @"netstandard.dll");

return MetadataReference.CreateFromFile(libraryPath);
}
}
}

关于c# - 运行时编译期间收到 “You must add a reference to assembly ' netstandard'”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62223131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com