1

Test.csprojは、TargetFrameworkVersionv4.0を参照System.XMLおよび指定します。 System.Xml.XmlReaderSettingsプロパティを取得し、その間DtdProcessingに非推奨になりました。ProhibitDtd要件は、両方のアセンブリをサポートする1​​つのプロジェクトからビルドすることです。

MSBuild 3.5(c:\ Windows \ Microsoft.NET \ Framework \ v3.5 \ MSBuild)を使用してビルドすると、C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ System.Xml.dll(ランタイムバージョンv2)を参照することになります。 .0.50727、バージョン2.0.0.0)

MSBuild 4.0(c:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ MSBuild)を使用してビルドすると、C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0\System.Xml.dllを参照することになります。 (ランタイムバージョンv4.0.30319、バージョン4.0.0.0)。

私が試みた修正は、TargetFrameworkVersionに基づいて.csprojファイルに定数を定義することでした。ただし、TargetFrameworkVersionは、どのアセンブリがリンクされるかを示すものではありません。

したがって、これは機能しません。

<PropertyGroup Condition=" '$(TargetFrameworkVersion)' != 'v2.0' And '$(TargetFrameworkVersion)' != 'v3.5' And '$(TargetFrameworkVersion)' != '' ">
  <DefineConstants Condition=" '$(DefineConstants)' != '' ">$(DefineConstants);</DefineConstants>
  <DefineConstants>$(DefineConstants)XMLDTDPROCESSING</DefineConstants>
</PropertyGroup>

このCSharpコードで:

namespace TestVSMSBuild
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Xml.XmlReaderSettings readerSettings = new System.Xml.XmlReaderSettings();
#if XMLDTDPROCESSING
            readerSettings.DtdProcessing = System.Xml.DtdProcessing.Ignore;
#else // !XMLDTDPROCESSING
            readerSettings.ProhibitDtd = false;
#endif // XMLDTDPROCESSING
            System.Console.WriteLine("Reader: '{0}'", readerSettings);
        }
    }

プロパティC:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targetsのコメントを読むことは次のとおりです。AssemblySearchPaths

<!--
The SearchPaths property is set to find assemblies in the following order:

    (1) Files from current project - indicated by {CandidateAssemblyFiles}
    (2) $(ReferencePath) - the reference path property, which comes from the .USER file.
    (3) The hintpath from the referenced item itself, indicated by {HintPathFromItem}.
    (4) The directory of MSBuild's "target" runtime from GetFrameworkPath.
        The "target" runtime folder is the folder of the runtime that MSBuild is a part of.
    (5) Registered assembly folders, indicated by {Registry:*,*,*}
    (6) Legacy registered assembly folders, indicated by {AssemblyFolders}
    (7) Look in the application's output folder (like bin\debug)
    (8) Resolve to the GAC.
    (9) Treat the reference's Include as if it were a real file name.
-->
<AssemblySearchPaths Condition=" '$(AssemblySearchPaths)' == '' ">
    {CandidateAssemblyFiles};
    $(ReferencePath);
    {HintPathFromItem};
    {TargetFrameworkDirectory};
    {Registry:$(FrameworkRegistryBase),$(TargetFrameworkVersion),$(AssemblyFoldersSuffix)$(AssemblyFoldersExConditions)};
    {AssemblyFolders};
    {GAC};
    {RawFileName};
    $(OutDir)
</AssemblySearchPaths>

私の推測では、これは次のようにGetFrameworkPaths定義されていることと関係があります。

<!--
============================================================
                                    GetFrameworkPaths

Get the paths for the .NET Framework installation directory, and the .NET Framework
SDK installation directory.

These paths are not used directly by this .targets file but are available for pre and
post build steps.
============================================================
-->
<PropertyGroup>
    <Framework35Dir>@(_TargetFramework35DirectoryItem)</Framework35Dir>
    <Framework30Dir>@(_TargetFramework30DirectoryItem)</Framework30Dir>
    <Framework20Dir>@(_TargetFramework20DirectoryItem)</Framework20Dir>
    <FrameworkDir>@(_TargetFramework20DirectoryItem)</FrameworkDir>

    <FrameworkSDKDir>@(_TargetFrameworkSDKDirectoryItem)</FrameworkSDKDir>
    <GetFrameworkPathsDependsOn></GetFrameworkPathsDependsOn>
</PropertyGroup>
<Target
    Name="GetFrameworkPaths"
    DependsOnTargets="$(GetFrameworkPathsDependsOn)">

    <!-- Get the paths to all of the target .NET framework directories. -->
    <GetFrameworkPath>
        <Output Condition=" '$(TargetFrameworkVersion)' == 'v3.5' "                                                                                  TaskParameter="FrameworkVersion35Path"  ItemName="_CombinedTargetFrameworkDirectoriesItem" />
        <Output Condition=" '$(TargetFrameworkVersion)' == 'v3.0' or '$(TargetFrameworkVersion)' == 'v3.5' "                                         TaskParameter="FrameworkVersion30Path"  ItemName="_CombinedTargetFrameworkDirectoriesItem" />
        <Output Condition=" '$(TargetFrameworkVersion)' == 'v2.0' or '$(TargetFrameworkVersion)' == 'v3.0' or '$(TargetFrameworkVersion)' == 'v3.5'" TaskParameter="FrameworkVersion20Path"  ItemName="_CombinedTargetFrameworkDirectoriesItem" />
        <Output TaskParameter="FrameworkVersion35Path"  ItemName="_TargetFramework35DirectoryItem" />
        <Output TaskParameter="FrameworkVersion30Path"  ItemName="_TargetFramework30DirectoryItem" />
        <Output TaskParameter="FrameworkVersion20Path"  ItemName="_TargetFramework20DirectoryItem" />
    </GetFrameworkPath>

    <PropertyGroup>
        <TargetFrameworkDirectory>@(_CombinedTargetFrameworkDirectoriesItem)</TargetFrameworkDirectory>
    </PropertyGroup>

    <!-- Get the path to the target .NET framework SDK directory. -->
    <GetFrameworkSdkPath>
        <Output TaskParameter="FrameworkSdkVersion35Path" PropertyName="TargetFrameworkSDKDirectory"/>
        <Output TaskParameter="FrameworkSdkVersion35Path" ItemName="_TargetFrameworkSDKDirectoryItem"/>
    </GetFrameworkSdkPath>

</Target>

System.XMLそれで、使用されているアセンブリに基づいてCSharpコードを変更する簡単な方法はありますか?このアプローチが正しい場合、定数を定義するためにどのような条件を使用する必要がありますか?XMLDTDPROCESSING

4

1 に答える 1

0

これが私がやったことです。System.XMLアセンブリを解決してから、バージョン2.0.0.0を具体的に確認します。

System.XMLアセンブリパスがSystem.XMLで終わると仮定していることに注意してください。ReferencePathを対応するReferenceに結び付ける方法はないようです。

<!-- System.XML.XmlReaderSettings changed after version 2.0.0.0.
     Define a constant to recognised that.
 -->
<Target Name="SetupXMLDtdProcessing">
  <GetAssemblyIdentity AssemblyFiles="%(ReferencePath.Identity)"
                       Condition=" '%(ReferencePath.Filename)' == 'System.XML' "
  >
    <Output TaskParameter="Assemblies" ItemName="_SystemXMLAssemblyIdentityItem" />
  </GetAssemblyIdentity>

  <PropertyGroup>
    <_SystemXMLAssemblyIdentity>%(_SystemXMLAssemblyIdentityItem.Identity)</_SystemXMLAssemblyIdentity>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(_SystemXMLAssemblyIdentity)' != 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
                           "
  >
    <DefineConstants Condition=" '$(DefineConstants)' != '' ">$(DefineConstants);</DefineConstants>
    <DefineConstants>$(DefineConstants)XMLDTDPROCESSING</DefineConstants>
  </PropertyGroup>
</Target>

<PropertyGroup>
  <CompileDependsOn>
    SetupXMLDtdProcessing;
    $(CompileDependsOn)
  </CompileDependsOn>
</PropertyGroup>
于 2012-11-09T12:38:34.937 に答える