简体   繁体   English

如何在自定义 Nuget 包中确定要使用的平台配置?

[英]How to determine which platform configuration to use from within a custom Nuget package?

I have a class that I need to put in a nuget package for internal use.我有一个类,我需要将其放入 nuget 包中以供内部使用。 This class needs to access some configuration settings but can potentially be installed in a Framework or Core application, so I don't know if it's a web.config, an app.config or an appsettings.json.这个类需要访问一些配置设置,但可能会安装在框架或核心应用程序中,所以我不知道它是 web.config、app.config 还是 appsettings.json。 Doing web searches for this just turns up articles on how to configure nuget (not how to get a nuget to read from the config files).对此进行网络搜索只会找到有关如何配置 nuget 的文章(而不是如何从配置文件中读取 nuget)。 Can anyone point me in the right direction?任何人都可以指出我正确的方向吗?

If your package targets both .NET Framework and .NET Core, or .NET Standard 2.0 or earlier, and you want to detect the target runtime when you compile the code, you could use a preprocessor directive in your source code, eg:如果您的包同时面向 .NET Framework 和 .NET Core,或者 .NET Standard 2.0 或更早版本,并且您希望在编译代码时检测目标运行时,则可以在源代码中使用预处理器指令,例如:

#if (NETCOREAPP1_0 || NETCOREAPP1_1 || NETCOREAPP2_0 || NETCOREAPP2_1)
    //.NET Core specific code...
#else
    //other...
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM