【问题标题】:How to make visual studio project use <ReferencePackage> or DLL reference And <ProjectReference> together to switch between Debug and DLL Release?如何使Visual Studio项目使用<ReferencePackage>或DLL引用和<ProjectReference>一起在Debug和DLL Release之间切换?
【发布时间】:2022-02-21 03:19:03
【问题描述】:

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

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

https://github.com/RicoSuter/NuGetReferenceSwitcher

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

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

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

简而言之,我需要让一个类库项目只能在我这边修改/编辑。其他人只使用 DLL 引用或 Nuget 包。

我需要做什么?

  1. 我需要创建一个主项目,它在我身上用作公共源,而其他人用作 DLL,只是不可调试。
  2. 如果我选择 MSBuild 配置(调试模式)而不是 Nuget DLL,则必须查看类库项目。但我需要修改整个核心源。然后再次将其重新发布为 Nuget,以允许其他人私下使用它。
  3. 我猜是主项目 .csproj 文件中的全部内容。我需要修改它以允许配置在其他开发人员可见的 Nuget 构建和仅对我可见的调试之间切换。选择它时, &lt;ProjectReference&gt;应该加载并应在我的解决方案中看到。
  4. 您可以忽略我提到的 Github 内容。一个 repo 可以是私有的/公共的,没有问题。

问题简短描述:

  1. 我在解决方案 A 中有主要项目。(必须对其他人公开 GIT)
  2. 我在解决方案 B 中有类库(可能在 2 个主要项目中使用)(必须是 GIT 私有 repo 仅供我使用)
  3. 我需要的类库源代码只对我自己可见,其他人看不到。他们只看到包或 DLL。
  4. 主要项目是公共Git repo,而类库是私有Git repo。
  5. 就我而言,我需要设置 2 种类型的 MSBuild 配置。 (Debug/) 和 (Nuget/)
  6. 其他人只允许使用私有nuget包,不得调试类库。
  7. 我只需要我使用 (Debug/&lt;ProjectReference&gt;) -> 这样我就可以直接更改类库并为其他人构建 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可执行文件),第二(FooClassLibrary)

  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 代码中的问题。现在类库引用了两个 MSBuild 配置(调试和发布)。但是我没有在Debug MSBuild的FooProject解决方案中添加Class Libray。

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

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

我不知道为什么解决方案还应该引用 ClassLibrary 项目,或者在 FooProject 中执行 &lt;ReferenceProject&gt; 时无法修改它。调试时收到以下错误消息:

我需要改变什么吗?

【问题讨论】:

    标签: c# visual-studio dll nuget visual-studio-2022


    【解决方案1】:

    目前没有官方解决方案。

    所以如果有人需要我收集的方法,我修改了一些标签以防止 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>
    

    随意修改或编辑上面的脚本或编辑答案。如果您找到更好的答案,请发布(例如,您为此创建了新的扩展名或其他内容)。

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 2013-06-06
      • 2021-05-12
      • 2016-10-15
      • 1970-01-01
      • 2016-06-08
      相关资源
      最近更新 更多