gpt4 book ai didi

c# - .Net Core - CS0012 'Object' 在未引用的程序集中定义

转载 作者:行者123 更新时间:2023-11-30 20:34:37 27 4
gpt4 key购买 nike

我是 .Net Core 的新手,我正在尝试基于它构建构建系统。作为该项目的一部分,我创建了一个抽象类来阐明构建任务应实现的内容,并将其填充到共享库中。

可执行项目引用此库并扫描项目目录以查找特殊命名的目录,然后检查其中是否有任何 .cs 文件。加载这些脚本,然后尝试使用通过 Microsoft.CodeAnalysis 和 friend 提供的工具进行编译。

在这样的背景下,我在编译阶段遇到了一个奇怪的问题:

如果我尝试将包含抽象类的共享库作为引用提供给编译过程,我会收到以下错误:

Failed CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.

接着是一堆关于预定义类型的提示:

CS0518: Predefined type 'System.Boolean' is not defined or imported CS0518: Predefined type 'System.Boolean' is not defined or imported CS0518: Predefined type 'System.Void' is not defined or imported ... etc etc.

但是,如果我省略引用,而是将每个共享库的源文件解析为语法树并将它们传递给编译过程,那么整个过程都会成功,并且我会得到一个返回的内存中程序集,我可以从中提取类型并实例化。

我已经阅读了 Google 必须提供的有关此错误的所有内容,但我不知所措。有人可以告诉我为什么会发生这种情况,并为我如何实现最初的目标(简单地链接到一个公共(public)共享库中)提供额外的互联网积分吗?

相关代码

    CSharpParseOptions parseOptions = CSharpParseOptions.Default;

SyntaxTree jobSyntaxTree = CSharpSyntaxTree.ParseText(scriptContents, parseOptions);

string generatedAssemblyName = Path.GetRandomFileName();
var referencedAssemblies = Assembly.GetEntryAssembly().GetReferencedAssemblies();

foreach(var referencedAssembly in referencedAssemblies)
{
var rfas = Assembly.Load(referencedAssembly);
references.Add(MetadataReference.CreateFromFile(rfas.Location));
}

var op = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

CSharpCompilation compilation = CSharpCompilation.Create(
generatedAssemblyName,
syntaxTrees: new[] { jobSyntaxTree },
references: references,
options: op);

var ms = new MemoryStream();

EmitOptions emitOptions = new EmitOptions();

EmitResult result = compilation.Emit(ms);

if(result.Success)
{
// Yay
}
else
{
// Boo-hoo
}

共享库的 Project.json 文件

{
"title": "BuildBotCore",
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": false,
"preserveCompilationContext": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"System.Runtime": "4.1.0",
"System.Runtime.Loader": "4.0.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "netcore50",
"buildOptions": {
"preserveCompilationContext": true
}
}
},
"configurations": {
"Debug": {
"buildOptions": {
"define": [
"DEBUG",
"TRACE"
],
"optimize": false,
"preserveCompilationContext": true
}
},
"Release": {
"buildOptions": {
"define": [
"RELEASE",
"TRACE"
],
"optimize": true,
"preserveCompilationContext": true
}
}
}
}

主要可执行文件的 Project.json

{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"System.Runtime": "4.1.0",
"Microsoft.CodeAnalysis.CSharp": "1.3.2",
"System.Runtime.Loader": "4.0.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"BuildBotCore": "1.0.0-*"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "netcore50",
"buildOptions": {
"preserveCompilationContext": true
}
}
},
"configurations": {
"Debug": {
"buildOptions": {
"define": [
"DEBUG",
"TRACE"
],
"optimize": false,
"preserveCompilationContext": true
}
},
"Release": {
"buildOptions": {
"define": [
"RELEASE",
"TRACE"
],
"optimize": true,
"preserveCompilationContext": true
}
}
}
}

最佳答案

长话短说
在一天结束时,您需要传递对“mscorlib.dll”及其私有(private)变体的引用。这将使上述编译错误消失。这在 .net core 中也是跨平台的,因为我已经在 Linux 和 Windows 上测试了这个解决方案。

完整版和代码示例
由于我正在等待帐户删除,因此我将发布最后一个答案。

解决方案是根据某些其他类型的程序集的位置手动包含 mscorlib.dll。像这样:

// Get the directory of a core assembly. We need this directory to
// build out our platform specific reference to mscorlib. mscorlib
// and the private mscorlib must be supplied as references for
// compilation to succeed. Of these two assemblies, only the private
// mscorlib is discovered via enumerataing assemblies referenced by
// this executing binary.
var dd = typeof(Enumerable).GetTypeInfo().Assembly.Location;
var coreDir = Directory.GetParent(dd);

List<MetadataReference> references = new List<MetadataReference>
{
// Here we get the path to the mscorlib and private mscorlib
// libraries that are required for compilation to succeed.
MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "mscorlib.dll"),
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location)
};

还有第二个 mscorelib,但它的名称以“private”为前缀。这是由执行程序集自动引用的,因此无需像上面那样手动生成其路径。

当您从正在执行动态代码编译的执行程序集中提取引用时,您将获得此私有(private) mscorlib。像这样:

// Enumerate all assemblies referenced by this executing assembly
// and provide them as references to the build script we're about to
// compile.
var referencedAssemblies = Assembly.GetEntryAssembly().GetReferencedAssemblies();
foreach(var referencedAssembly in referencedAssemblies)
{
var loadedAssembly = Assembly.Load(referencedAssembly);

references.Add(MetadataReference.CreateFromFile(loadedAssembly.Location));
}

现在这些引用已经组装好,您可以简单地将它们传递给您的代码编译:

// Initialize the compilation with our options, references and the
// already parsed syntax tree of the build script.
CSharpCompilation compilation = CSharpCompilation.Create(
generatedAssemblyName,
syntaxTrees: new[] { jobSyntaxTree },
references: references,
options: op);

// Compile and emit new assembly into memory.
var ms = new MemoryStream();
EmitResult result = compilation.Emit(ms);

...瞧,您可以在 .Net 核心中使用 Roslyn 跨平台在代码内动态编译。

关于c# - .Net Core - CS0012 'Object' 在未引用的程序集中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943899/

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