【问题标题】:C# Switch dependencies depending on x86 or x64C# 根据 x86 或 x64 切换依赖项
【发布时间】:2021-03-03 18:46:38
【问题描述】:

我们有一个第三方 dll,它是 x86 或 x64,现在我们必须将 x64 dll 更改为依赖于 TargetPlatform 的 x86。谁能告诉我怎么做?

第一次尝试是使用构建后脚本覆盖 dll。

【问题讨论】:

    标签: c# visual-studio x86 dependencies 64-bit


    【解决方案1】:

    使用配置管理器,我们可以使用 x86、x64 标记构建,使用宏 $(PlatformName) 我们可以命名相对于平台 (x64/x86) 的 dll 路径

    重要的是,必须直接在 *.csproj 文件中设置宏,例如:

    <Reference Include="DLLNAME, Version=4.9.0.0, Culture=neutral, PublicKeyToken=TOKEN, 
          processorArchitecture=$(PlatformName)">
          <HintPath>Lib\$(PlatformName)\DLLNAME.dll</HintPath>
        </Reference>
    

    另外,如果有需要编译到主程序文件夹中的插件,则必须直接在 *.csproj 文件中更改构建路径。例如:

     <OutputPath>..\..\bin\$(PlatformName)\Debug\Features\</OutputPath>
    

    那是因为 VS 转义了 '$('')'

    编辑: 太快。 &lt;OutputPath&gt; 不理解宏。因此,我们必须更改每个 Build-Configuration 的输出路径。例如:

      <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
        <DebugSymbols>true</DebugSymbols>
        <OutputPath>..\..\bin\x86\Debug\Features\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DebugType>full</DebugType>
        <PlatformTarget>x86</PlatformTarget>
        <LangVersion>7.3</LangVersion>
        <ErrorReport>prompt</ErrorReport>
        <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
        <Prefer32Bit>true</Prefer32Bit>
      </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
        <OutputPath>..\..\bin\x86\Release\Features\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <Optimize>true</Optimize>
        <DebugType>pdbonly</DebugType>
        <PlatformTarget>x86</PlatformTarget>
        <LangVersion>7.3</LangVersion>
        <ErrorReport>prompt</ErrorReport>
        <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
        <Prefer32Bit>true</Prefer32Bit>
      </PropertyGroup>
    

    编辑 2:仅对于 x64 或 x86 依赖项,有 Condition-Attribute 可以将一些 cs 文件与 x86 或 x64 分开,例如:

    <Reference Condition="'$(Platform)'=='x64'" Include="STPadLibNet">
      <HintPath>Lib\x64\DLLNAME.dll</HintPath>
    </Reference>
    

    对于某些类别,您可以像这样使用它:

    <Compile Condition="'$(Platform)'=='x64'" Include="MyClass.cs" />
    

    【讨论】:

    • 很高兴得知您的问题已经解决,您可以点击“✔”将您的回复标记为答案,以帮助遇到类似问题的其他人。
    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2017-01-11
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多