VS2022从类库csproj获取主可执行文件的目标框架

问题描述 投票:0回答:1

我们需要从类库项目的csproj文件中获取Main可执行程序集的TargetFramework。这样类库就可以针对 net7.0-android。以下代码失败,并显示 XmlDocument 无法在 MSBuild 属性函数中执行。

我们怎样才能实现我们想要做的事情?

    <PropertyGroup>
        <MainExecutableTargetFramework>$([System.Xml.XmlDocument]::new().Load("$($(MSBuildStartupDirectory))/$(AssemblyName).csproj").SelectSingleNode("/Project/PropertyGroup/TargetFramework").InnerText)</MainExecutableTargetFramework>
    </PropertyGroup>

    <PropertyGroup Condition=" '$(MainExecutableTargetFramework)' == 'net7.0-android' ">
        <TargetFrameworks>net7.0-android</TargetFrameworks>
    </PropertyGroup>
visual-studio msbuild visual-studio-2022
1个回答
0
投票

正如乔纳森提到的,您的财产中没有使用过的用法:

属性函数语法

仅限以下类别:

1、String(实例)属性函数

2、静态属性函数

3、MSBuild属性函数

看一下静态属性函数,你会发现只允许以下:

1、这些系统类的公共静态方法或属性:

System.Byte
System.Char
System.Convert
System.DateTime
System.DateTimeOffset (Available in MSBuild 17.3 and higher)
System.Decimal
System.Double
System.Enum
System.Guid
System.Int16
System.Int32
System.Int64
System.IO.Path
System.Math
System.Runtime.InteropServices.OSPlatform
System.Runtime.InteropServices.RuntimeInformation
System.UInt16
System.UInt32
System.UInt64
System.SByte
System.Single
System.String
System.StringComparer
System.TimeSpan
System.Text.RegularExpressions.Regex
System.UriBuilder
System.Version
Microsoft.Build.Utilities.ToolLocationHelper

2、静态方法和属性:

System.Environment::CommandLine
System.Environment::ExpandEnvironmentVariables
System.Environment::GetEnvironmentVariable
System.Environment::GetEnvironmentVariables
System.Environment::GetFolderPath
System.Environment::GetLogicalDrives
System.Environment::Is64BitOperatingSystem
System.Environment::Is64BitProcess
System.Environment::MachineName
System.Environment::NewLine (Available in MSBuild 17.3 and higher)
System.Environment::OSVersion
System.Environment::ProcessorCount
System.Environment::StackTrace
System.Environment::SystemDirectory
System.Environment::SystemPageSize
System.Environment::TickCount
System.Environment::UserDomainName
System.Environment::UserInteractive
System.Environment::UserName
System.Environment::Version
System.Environment::WorkingSet
System.IO.Directory::GetDirectories
System.IO.Directory::GetFiles
System.IO.Directory::GetLastAccessTime
System.IO.Directory::GetLastWriteTime
System.IO.Directory::GetParent
System.IO.File::Exists
System.IO.File::GetAttributes
System.IO.File::GetCreationTime
System.IO.File::GetLastAccessTime
System.IO.File::GetLastWriteTime
System.IO.File::ReadAllText

所以 System.Xml.XmlDocument 不能在属性中使用。

这是处理XML文档的方法,你可以看一下:

从 MSBuild 读取 launchSettings.json 值?

© www.soinside.com 2019 - 2024. All rights reserved.