如何让visual studio项目使用<ReferencePackage>或DLL引用和<ProjectReference>一起在Debug和DLL Release之间切换?

How to make visual studio project use <ReferencePackage> or DLL reference And <ProjectReference> together to switch between Debug and DLL Release?

请问我有问题。抱歉,如果问题标题没有被很好地识别,我无法在我的声誉期间上传 full-image。

编辑: 我可能需要这样的东西

https://github.com/RicoSuter/NuGetReferenceSwitcher

https://github.com/0UserName/NuGetSwitcher

但上面的 repos 没有更新到 VS 2022?请帮忙?

编辑: 看起来其他人问过这个问题:https://github.com/dotnet/sdk/issues/1151

不久,我需要做一个 Class 库项目,可以 modify/edit 只在我身边。其他人只使用 DLL 引用或 Nuget 包。

我需要做什么?

  1. 我需要创建一个主项目,我自己用作 public 源代码,其他人用作 DLL,仅不可调试。
  2. 如果我选择 MSBuild 配置(调试模式)而不是 Nuget DLL,则必须查看 class 库项目。但我需要修改整个核心源代码。然后再次将其重新发布为 Nuget,以允许其他人私下使用它。
  3. 我猜主项目 .csproj 文件中的整个空白。我需要修改它以允许 Configuration 在对其他开发人员可见的 Nuget 构建和仅对我可见的 Debug 之间切换。选择它时,<ProjectReference> 应该加载并且应该在我的解决方案中可见。
  4. 你可以忽略我提到的 Github 东西。回购可以 private/public 没有问题。

问题简述:

  1. 我在解决方案 A 中有主要项目。(对于其他人必须是 GIT public)
  2. 我在解决方案 B 中有 class 库(可能在 2 个主要项目中使用)(必须是 GIT 私人仓库)
  3. 我需要 class 库源仅对我可见,对其他人不可见。他们只看到包或 DLL。
  4. 主要项目是 public Git 回购,而 class 库是私人 Git 回购。
  5. 在我的例子中,我需要设置两种类型的 MSBuild 配置。 (调试/) 和 (Nuget/)
  6. 其他人只允许使用私有 nuget 包,他们不得调试 class 库。
  7. 我只需要我自己使用 (Debug/<ProjectReference>) -> 所以我可以直接更改 class 库并为其他人构建 Nuget 包而无需包含 PDB 文件等。

我想做什么?我阅读了哪些主题?

  1. 我关注以下话题:Use local source code of NuGet package to debug and edit code@钱先生评论)

https://sitecore.stackexchange.com/questions/15293/what-are-packagereferences-and-how-will-they-help-optimise-the-way-i-deal-with-n/15294

  1. 我创建了两个解决方案。 第一个(FooProject 可执行文件),第二个(FooClass库)

  2. 我修改 FooProject.csproj 并添加以下行

     <ItemGroup Condition="'$(Configuration)' == 'Debug'">
       <ProjectReference 
       Include="..\..\FooClassLibrary\FooClassLibrary\FooClassLibrary.csproj">
           <Project>{a2329af5-316e-4339-8b56-d78aba72e919}</Project>
           <Name>FooClassLibrary</Name>
      </ProjectReference>
    </ItemGroup>
    <ItemGroup Condition="'$(Configuration)' == 'Release'">
      <Reference Include="FooClassLibrary">
      <HintPath>..\..\FooClassLibrary\FooClassLibrary\bin\Release\FooClassLibrary.dll> 
      </HintPath>
      </Reference>
    </ItemGroup>
    

编辑:

我修复了上面 .csproj 代码中的问题。现在 class 库引用了两个 MSBuild 配置(调试和发布)。但是我没有在 Debug MSBuild 的 FooProject 解决方案中添加 Class Libray。

现在我应该创建两个解决方案 *.sln 文件吗?一个用于 FooProject 并使用 FooClassLibrary 作为 non-modifying 调试的 DLL 引用? 第二个 FooProject 解决方案 (FooClassDebuggingAndEdit.sln) 将项目引用到 FooProject?

这里有两个解决方案*.rar link https://anonfiles.com/rdm8A4I2x9/TwoSolutions_rar

我不知道为什么解决方案还应该引用 ClassLibrary 项目,或者在 FooProject 中执行 <ReferenceProject> 时无法修改。调试时出现以下错误消息:

我需要改变什么吗?

目前还没有官方解决方案。

因此,如果有人需要我收集的方法,我会修改一些标签以防止 PackageName 与 ProjectName 发生冲突,这是最终解决方案,原件复制自 https://github.com/dotnet/sdk/issues/1151#issuecomment-459275750 非常感谢脚本作者:

这是它的增强版:

<PropertyGroup>
    <ReplacePackageReferences Condition="'$(ReplacePackageReferences)' == ''">true</ReplacePackageReferences>
    <ReplaceProjectReferences Condition="'$(ReplaceProjectReferences)' == ''">true</ReplaceProjectReferences>
</PropertyGroup>
<Choose>
    <When Condition="'$(SolutionPath)' != '' AND '$(SolutionPath)' != '*undefined*' AND Exists('$(SolutionPath)')">
        <PropertyGroup>
            <SolutionFileContent>$([System.IO.File]::ReadAllText($(SolutionPath)))</SolutionFileContent>
            <SmartSolutionDir>$([System.IO.Path]::GetDirectoryName( $(SolutionPath) ))</SmartSolutionDir>
            <RegexPattern>(?&lt;="[PackageName]", ")(.*)(?=", ")</RegexPattern>
            <HasSolution>true</HasSolution>
        </PropertyGroup>
    </When>
    <Otherwise>
        <PropertyGroup>
            <HasSolution>false</HasSolution>
        </PropertyGroup>
    </Otherwise>
</Choose>
<Choose>
    <When Condition="$(ReplacePackageReferences) AND $(HasSolution)">
        <ItemGroup>
            <!-- Keep the identity of the  packagereference -->
            <SmartPackageReference Include="@(PackageReference)">
                <InProject>false</InProject>
                <PackageName>%(Identity)</PackageName>
                <InSolution>$(SolutionFileContent.Contains('\%(Identity).csproj'))</InSolution>
            </SmartPackageReference>
            <!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
            <PackageInSolution Include="@(SmartPackageReference -&gt; WithMetadataValue('InSolution', True) )">
                <Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
                <SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$(SolutionFileContent)', '%(Pattern)' ))</SmartPath>
                <ProjName>'%(PackageName)'</ProjName>
            </PackageInSolution>
            <ProjectReference Include="@(PackageInSolution -> '$(SmartSolutionDir)\%(SmartPath)' )">
                <Name>@(PackageInSolution -&gt; %(ProjName))</Name>
            </ProjectReference>
            <!-- Remove the package references that are now referenced as projects -->
            <PackageReference Remove="@(PackageInSolution -> '%(PackageName)' )" />
        </ItemGroup>
    </When>
    <When Condition="$(ReplaceProjectReferences) AND '$(_RestoreSolutionFileUsed)' == ''">
        <ItemGroup>
            <!-- Keep the identity of the  project reference (relative path), determine the project name and whether the project is contained in the current solution -->
            <SmartProjectReference Include="@(ProjectReference)">
                <OriginalIdentity>%(Identity)</OriginalIdentity>
                <ProjectName>$([System.IO.Path]::GetFileNameWithoutExtension( $([System.IO.Path]::GetFullPath( '%(OriginalIdentity)' )) ))</ProjectName>
                <InSolution>$(SolutionFileContent.Contains('\%(ProjectName).csproj'))</InSolution>
            </SmartProjectReference>
            <!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
            <ProjectNotInSolution Include="@(SmartProjectReference -&gt; WithMetadataValue('InSolution', False) )">
            </ProjectNotInSolution>
            <!--Reference the latest version of the package (why not * ? > see https://github.com/NuGet/Home/issues/7328-->
            <PackageReference Include="@(ProjectNotInSolution -> '%(ProjectName)' )" Version="*" />
            <!-- Remove the project references that are now referenced as packages -->
            <ProjectReference Remove="@(ProjectNotInSolution -> '%(OriginalIdentity)' )" />
        </ItemGroup>
    </When>
</Choose>

请注意,如果您需要自动更新 nuget 包,则必须在上面提供的脚本上方指定包:

<ItemGroup>
      <PackageReferen``ce Include="FooClassLibrary" Version="*"/>
  </ItemGroup>

随时修改或编辑以上脚本或编辑答案。如果您找到更好的答案,请post它(例如,您为此创建了新的扩展名或其他东西)。