简体   繁体   中英

How can I identify the calling platform from a .NET Standard library at runtime?

Specifically, what I need to identify is the calling development platform (ie .NET Core vs. .NET Framework vs. Other) rather than the operating system information I can get through System.Environment.OperatingSystem.Platform . Is there a way to do this?

(To give more detail on my specific use case, I am referencing a library - log4net - which is configured by passing it an XML resource. Some of its configurable features which I want to use if available are supported under .NET Framework but not under .NET Core, and I want to be able therefore to pass it a different version of the resource depending on which I'm being used by.)

Solving from the runtime environment could prove to be uncertain as runtime change. I would force the issue up front before the application runs. As previously stated you could pass in the runtime or configure it.

Another option would be something similar to SL4J provides. Runtime determination dependent upon how you deploy the application.

You could create your own interface for exposing platform information. Then, at deploy time, just add the appropriate assembly to the installation. Load the assembly and call the methods you require.

Alternatively you see how Microsoft handled similar situations here: https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

I had to do this once. There was a method I needed to call that varied across implementations. I just used reflection to try one name after another.

Trying to detect the runtime library is silly. Detect the differences directly. It's less likely to break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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