我们有一个商业库,我正在努力将其移植到 .NET Core。我想保留其中的几个调用,以便仅在以 .NET 标准运行时使用。 (出于好奇,一组是读取 Windows 服务器上需要凭据才能访问的文件。)
有没有:
- 该调用将告诉我是否在 .NET Standard 和 .NET Core 下运行。
- 有没有办法让一个类只有在标准下运行时才会被实例化/调用,但 DLL 仍然可以在 Core 下正常加载?
也在MSDN上问过 https://social.msdn.microsoft.com/Forums/en-US/d96aa6e0-9238-4d76-9679-7cbd852f7000/what-is-the-best-way-to-have-net-framework-net-core-specific-code?forum=netfxbcl
正如您所描述的,拥有一个 nuget 包并能够根据 nuget 包安装到的框架指定不同的行为或依赖项,只能通过多目标 https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks我假设你正在这样做或将会这样做。
一旦你有指定的目标框架 https://learn.microsoft.com/en-us/dotnet/standard/frameworks#how-to-specify-target-frameworks,您可以在预编译块中使用预定义的变量:
#if NETFRAMEWORK
// use full framework class here. You were installed into a full framework app or library
#elif NETCOREAPP
// use .NET Core class here. You were installed into a .NET Core app or library
#else NETSTANDARD
// uh... okay... you were installed into another .NET Standard library,
// we still have no idea where *that* might be installed... help?
// Maybe make it configurable after all?
#endif
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)