Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net core和.net framework 4.6的Nuget包兼容性_C#_.net Core_Nuget Package_.net Framework Version - Fatal编程技术网

C# .net core和.net framework 4.6的Nuget包兼容性

C# .net core和.net framework 4.6的Nuget包兼容性,c#,.net-core,nuget-package,.net-framework-version,C#,.net Core,Nuget Package,.net Framework Version,我创建了一个nuget包,它支持.NETFramework和.NETCore。 但其中有一个类的引用仅适用于.NETCore。 我不需要在.net框架中提供该类 在为.net framweork构建包时,是否有任何属性或方法可用于排除该类 这就是我的目标框架 <TargetFrameworks>netcoreapp2.0;netstandard2.0;net45;net46</TargetFrameworks> netcoreapp2.0;netstandard2.0;

我创建了一个nuget包,它支持.NETFramework和.NETCore。 但其中有一个类的引用仅适用于.NETCore。 我不需要在.net框架中提供该类

在为.net framweork构建包时,是否有任何属性或方法可用于排除该类

这就是我的目标框架

<TargetFrameworks>netcoreapp2.0;netstandard2.0;net45;net46</TargetFrameworks>
netcoreapp2.0;netstandard2.0;net45;网络46

只需使用根据每个目标自动提供的条件编译符号即可。例如:

// Put this at the start of the file (if you want some of the file to still be built)
// or class (if you just want to exclude a single class). You could even
// exclude just a single method, or part of a method.
#if NETCOREAPP2_0 || NETSTANDARD2_0

public class NetFrameworkOnlyClass
{
}

// And this at the end
#endif
或者,您可以这样排除,而不是包括:

#if !NET45 && !NET46

有关在每个环境中定义的条件编译符号的列表,请参见。(我认为最好有版本中立的“
NETCORE
”、“
NETSTANDARD
”和“
NETFULL
”符号或类似符号,但我认为不存在这些符号。如果您愿意,可以在项目文件中指定它们。)

另外,您确实需要以不同的方式针对net45和net46吗?netcoreapp2.0和netstandard2.0也是如此?如果您的目标是net45,那么该包将与net46一起工作——同样,如果您的目标是netstandard2.0,那么该包将与netcoreapp2.0一起工作。如果您想使用net46或netcoreapp2.0特定功能,通常只需要针对所有这些功能。现在删除屏幕截图可能是值得的,因为它不再相关。Daisy,添加您的方式后,请参阅我问题的屏幕截图中的附加图像。还没修好error@Diana:您已使用指令将其放在
之后,这意味着如果这些命名空间不可用,它将失败。假设您不希望netstandard/netcore的文件中有任何内容,请将其移到文件开头的右侧。@Diana:尽管错误消息表明您可能对配置和配置扩展的正确包没有依赖性。您是否有对Microsoft.Configuration.Extensions的包引用?您还需要添加条件编译符号以使用语句。以serilog的LogContext为例:Daisy谢谢。成功了。我把你的答案标记为一个很好的解决方案:)