.NET Core类库的条件编译符号 [英] Conditional compilation symbol for a .NET Core class library
问题描述
我已经创建了一个.NET Core R2类库,并且具有一些用于多个不同平台的通用代码。
其中一些代码无效.NET Core平台,因此我希望将其包装在条件编译符号中。首先,我在互联网上搜索了是否可以找到内置符号(例如SILVERLIGHT表示
请注意,我在 NET_CORE
值中添加了有条件的编译符号,但是当我在代码中使用它时,代码不会被忽略。
-
是否有内置的NET Core平台(我正在使用R2)的符号?
-
如果没有,我创建自己的符号会出错吗? p>
.xproj文件:
<?xml version = 1.0 encoding = utf-8?>
< Project ToolsVersion = 14.0 DefaultTargets = Build xmlns = http://schemas.microsoft.com/developer/msbuild/2003>
< PropertyGroup>
< VisualStudioVersion Condition ='$(VisualStudioVersion)'==‘’> 14.0< / VisualStudioVersion>
< VSToolsPath Condition ='$(VSToolsPath)'==‘’> $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v $(VisualStudioVersion)< / VSToolsPath>
< / PropertyGroup>
<导入项目= $(VSToolsPath)\DotNet\Microsoft.DotNet.Props Condition ='$(VSToolsPath)'!=’ />
< PropertyGroup Label = Globals>
< ProjectGuid> 253184d7-9b42-4233-a871-8cfa3ee9e83e< / ProjectGuid>
< RootNamespace> Linq2Db.NetCore< / RootNamespace>
< BaseIntermediateOutputPath Condition =’$(BaseIntermediateOutputPath)’==’’> .\obj< / BaseIntermediateOutputPath>
< OutputPath Condition =’$(OutputPath)’==’> .\bin\< / OutputPath>
< TargetFrameworkVersion> v4.6.1< / TargetFrameworkVersion>
< SccProjectName> SAK< / SccProjectName>
< SccProvider> SAK< / SccProvider>
< SccAuxPath> SAK< / SccAuxPath>
< SccLocalPath> SAK< / SccLocalPath>
< / PropertyGroup>
< PropertyGroup>
< SchemaVersion> 2.0< / SchemaVersion>
< / PropertyGroup>
<导入项目= $(VSToolsPath)\DotNet\Microsoft.DotNet.targets Condition ='$(VSToolsPath)'!=’ />
< / Project>
更新:我能够使用提供的答案中的链接解决此问题。这里是详细信息...
最初 project.json
文件看起来像这样...
{
dependencies:{
NETStandard.Library: 1.5.0-rc2-24027
},
frameworks:{
netstandard1.5:{
imports: dnxcore50
}
},
buildOptions:{
defines:[ NET_CORE]
}
}
我通过将其更改为以下内容来解决了这个问题...
{
frameworks:{
netstandard1.5:{
imports: dnxcore50,
dependencies:{
NETStandard.Library: 1.5。 0-rc2-24027
},
buildOptions:{
define:[ NET_CORE]
}
}
}
}
应在您的应用中定义条件变量RC2的project.json文件,我在这里有一个示例项目,
但是本文中还有一些预定义的
I have created a .NET Core R2 class library and have some common code that I use for several different platforms.
Some of the code is not valid in the .NET Core platform and so I wish to wrap it around a conditional compilation symbol. I first searched the Internet to see if I could find a built-in symbol (like SILVERLIGHT for Silverlight applications and WINFX_CORE for Windows 8 applications), but I was not able to find any information, so I decided to create my own symbol. This also does not seem to work.
From everything I read, adding and using a symbol should be easy. Just add a value to the conditional compilation symbols in the project properties → Build tab. I did that, but it does not seem to work. Here are a couple of screenshots...
Notice that I added a NET_CORE
value in the conditional compilation symbol, but when I use it in code the code is not being ignored.
Is there is a built-in symbol for the .NET Core platform (I am using R2)?
If there is not one, what am I doing wrong creating my own symbol?
The .xproj file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>253184d7-9b42-4233-a871-8cfa3ee9e83e</ProjectGuid>
<RootNamespace>Linq2Db.NetCore</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
UPDATE: I was able to resolve this using the link in the provided answer. Here are the details...
Originally the project.json
file looked like this...
{
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50"
}
},
"buildOptions": {
"defines": [ "NET_CORE" ]
}
}
I resolved the issue by changing it to this...
{
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
},
"buildOptions": {
"define": [ "NET_CORE" ]
}
}
}
}
Conditional variables should be defined in your project.json file for RC2, and I have a sample project here,
Port #SNMP from .NET Core RC1 to RC2
But there are also predefined ones from this article,
Developing Libraries with Cross Platform Tools
这篇关于.NET Core类库的条件编译符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!