dotnet / standard

This repo is building the .NET Standard
3.06k stars 427 forks source link

Should NOT allow a .NET Standard class library to reference platform extensions. #259

Closed jarenduan closed 6 years ago

jarenduan commented 7 years ago

I read here that in .Net Standard 2.0, a .NET Standard class library is able to reference non-.NET Standard class libraries, like registry, as platform extensions.

IMHO, I think we should not allow a .NET Standard class library to reference platform extensions, and if the developer really need them, should reference them in a multi-target library.

From my point of view, if a .NET Standard class library manually references the registry assembly, the library is not a "standard" library any more, because it requires platform supporting, and it actually becomes a "platform-specific" library. I admit referencing non-.NET Standard class library as platform extensions is very useful and powerful, but it just doesn't make sense doing that in a .NET Standard class library.

If someone really would like to do that, for example, there are only a few features/APIs in the library that depend on registry, and the majorities are platform independent ones, he should build a multi-targeting project, and reference registry assembly when targeting traditional .net framework, for example , just like:

#if net45
      // FeaturesUsingRegistry
#endif 
      // OtherStandardFeatures

And get two Dlls in the package, one for net45, which includes all the features, and the other one is for netstandard20 excluding the registry related features, which can run on all the platforms. And of course, the Nuget would get the proper one for consumers.

BTW, if I understand it correctly, when a .Net Core application consumes a .NET Standard class library referencing registry assembly, it will compile successfully and may throw "PlatformNotSupported" exception during the runtime, right? By using multi-targeting, it will free platforms from "PlatformNotSupported" exceptions.

benaadams commented 7 years ago

There's a great set of videos from @terrajobst that explains this further https://www.youtube.com/watch?v=fOzcVwzkGP0&list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY

jarenduan commented 7 years ago

@benaadams , well, I put it wrong way. What I meant is we should not allow netstandard library to reference platform-specific assemblies.

terrajobst commented 6 years ago

No, that's by-design. If you want to block this you can use the platform-compat tool and configure the the warnings as errors.