gpt4 book ai didi

c# - 创建 NuGet 包时,如何为程序集指定框架版本?

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:01 24 4
gpt4 key购买 nike

我正在尝试使用 NuGet 核心创建一个 NuGet 包作为构建过程的一部分。构建过程会创建特定的框架版本 3.5、4.0 和 4.5。如何为每个程序集指定框架版本?

public void Create(Package package)
{
if (String.IsNullOrEmpty(package.Id))
throw new ArgumentException("package.Id is required", "package");

if (String.IsNullOrEmpty(package.Version))
throw new ArgumentException("package.Version is required", "package");

if (String.IsNullOrEmpty(package.Authors))
throw new ArgumentException("package.Authors is required", "package");

if (String.IsNullOrEmpty(package.Description))
throw new ArgumentException("package.Description is required", "package");

if (package.Files == null || package.Files.Count == 0)
throw new ArgumentException("package.Files cannot be empty", "package");

ManifestMetadata metadata = new ManifestMetadata();
metadata.Id = package.Id;
metadata.Version = package.Version;
metadata.Title = package.Title;
metadata.Authors = package.Authors;
metadata.Owners = package.Authors;
metadata.IconUrl = package.IconUrl;
metadata.ProjectUrl = package.ProjectUrl;
metadata.RequireLicenseAcceptance = true;
metadata.LicenseUrl = package.LicenseUrl;
metadata.Summary = package.Summary;
metadata.Description = package.Description;
metadata.Copyright = package.Copyright;
metadata.Language = "en-US";
metadata.Tags = package.Tags;
metadata.ReleaseNotes = package.ReleaseNotes;

List<ManifestFile> manifestFiles = new List<ManifestFile>();

foreach (var file in package.Files)
{
ManifestFile manifestFile = new ManifestFile();
manifestFile.Source = Path.GetFileName(file);
manifestFile.Target = @"lib\" + Path.GetFileName(file);
manifestFiles.Add(manifestFile);
}

PackageBuilder builder = new PackageBuilder();
builder.PopulateFiles(ExtractPath(package.Files[0]), manifestFiles);
builder.Populate(metadata);

using (FileStream stream = File.Open(package.PackagePath, FileMode.OpenOrCreate))
{
builder.Save(stream);
}
}

private static string ExtractPath(string fullPath)
{
if (fullPath.Length == 0)
throw new ArgumentNullException("fullPath");

//Account for already in form of path
if (String.IsNullOrEmpty(Path.GetFileName(fullPath))
|| String.IsNullOrEmpty(Path.GetExtension(fullPath)))
{
return fullPath;
}

return Path.GetDirectoryName(fullPath);
}

最佳答案

按照惯例,在定义目标时,您使用根目录,然后是框架版本。这是概述here .

因此在 nuspec 文件中,对于 .NET 4.5,您会看到:

  <files>
<file src="bin\Release\MyAssembly.dll" target="lib\net45" />
</files>

因此,在定义您的目标时,只需添加您所针对的 .NET 版本的约定:

var netVersion45 = "\net45";
foreach (var file in package.Files)
{
ManifestFile manifestFile = new ManifestFile();
manifestFile.Source = Path.GetFileName(file);
manifestFile.Target = @"lib\" + netVersion45;
manifestFiles.Add(manifestFile);
}

关于c# - 创建 NuGet 包时,如何为程序集指定框架版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045217/

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