使用 .NET Core CLI,在面向完整框架时添加对本地 DLL 的引用 [英] Using .NET Core CLI, add a reference to a local DLL when targeting the full framework
问题描述
我想知道在使用 .NET Core 工具构建新的控制台应用程序并且仅针对 .NET 4.5.1 框架时,如何引用本地的、未签名的 CLR 4.0 程序集.
I would like to know how to reference a local, unsigned CLR 4.0 assembly, when building a new Console application using the .NET Core tools and only targeting the .NET 4.5.1 framework.
我在 Windows 10 机器上执行了以下操作:
I did the following on a Windows 10 machine:
- 使用以下代码创建一个新的 MyLibrary.cs:
using System;
namespace MyNamespace
{
public class MyLibrary
{
public static void SayHi()
{
Console.WriteLine("Hi");
}
}
}
使用
csc/t:library MyLibrary.cs
编译以上代码.
创建一个新文件夹和一个 .NET Core 项目.
Create a new folder and a .NET Core project.
mkdir MyConsoleApp
cd MyConsoleApp
dotnet new
- 编辑自动生成的 project.json 文件以面向 .NET 4.5.1.
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"net451" : { }
}
}
- 测试项目是否构建并运行.确实如此.
dotnet restore
dotnet build
binDebug
et451MyConsoleApp.exe
- 编辑 Program.cs 以调用 MyLibrary.dll 中的代码.
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
MyNamespace.MyLibrary.SayHi();
}
这就是我被困的地方.
我应该将 DLL 放在哪里以及在 project.json 中放什么,以便我可以在我的程序集中调用代码?
Where do I place my DLL and what do I put in the project.json so that I can call code in my assembly?
推荐答案
旧 DLL 必须包装在 nuget 包中.我以前从未创建过 nuget 包.在这种情况下,我是这样做的:
The legacy DLL must be wrapped in a nuget package. I have never created a nuget package before. Here is how I did it in this case:
1. 准备为旧 DLL 创建 nuget 包:
1. Prepare to create a nuget package for the legacy DLL:
cd..
mkdir lib
et40
move MyLibrary.dll lib
et40
nuget spec -AssemblyPath lib
et40MyLibrary.dll
// which generates a MyLibrary.dll.nuspec XML file.
2. 填写或删除 nuspec 文件中的字段:
2. Fill in or delete fields in the nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyLibrary.dll</id>
<version>1.0.0</version>
<authors>Wallace Kelly</authors>
<description>My test library.</description>
</metadata>
</package>
3. 打包 nuget 包并将其移动到 nuget 文件夹结构中:
3. Pack the nuget package and move it into the nuget folder structure:
nuget pack MyLibrary.nuspec -OutputDirectory lib
et40
// which generates a MyLibrary.dll.1.0.0.nupkg
4. 创建一个新的 nuget-source 文件夹:
4. Create a new nuget-source folder:
nuget init lib nuget-source
5. 在项目文件夹(例如 MyConsoleApp)中,使用以下内容创建一个新的 nuget.config 文件:
5. In the project folder (e.g, MyConsoleApp), create a new nuget.config file with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
</configuration>
6. 将您的本地 nuget-source 文件夹添加到 nuget 的源列表中:
6. Add your local nuget-source folder to the list of nuget's sources:
nuget sources add -Name "Local Source" -Source "c: empdotnet-core-test
uget-source" -configfile nuget.config
在 project.json 文件中,添加对旧 DLL 的依赖项.
In the project.json file, add a dependency to the legacy DLL.
"dependencies": {
"MyLibrary.dll": "1.0.0"
},
7.此时,您应该能够恢复、构建和运行项目.
7. At this point, you should be able to restore, build, and run the project.
dotnet restore
dotnet build
dotnet run
HT:感谢@michael-kennedy 帮助我解决这个问题.
HT: Thanks to @michael-kennedy for helping me work this out.
这篇关于使用 .NET Core CLI,在面向完整框架时添加对本地 DLL 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!