Bait and switch with .NET Standard

Simple question that I can test for myself later but…

Can I use the PCL Bait and switch trick with .NET Standard library as well?

About Bait and Switch:

What I try to achieve:
Using #ifdef and shared code I already have multiple version of my refection library. Using Emit on .NET Framework and .NET Core. And using Reflection on Xamarin (Android/iOS). And of course I also have a PCL version used only at compile time.
Could I use a (new) .NET Standard version at compile time and replace it with the target framework version at runtime, as I do for PCL?

I think it should work, but I haven’t tried it either.

Though there is a difference, the article says:

If you take this to its logical conclusion, you realize that, if you provide an Assembly for every platform in the PCL, this means that nobody will ever execute the code in the PCL version, only the platform versions.

The difference is that there is no fixed list of platforms that support a certain version of .Net Standard. A platform can appear tomorrow that supports the version of .Net Standard you chose and that you couldn’t have known about when you wrote your library, so you couldn’t have provided a separate assembly for that platform.

1 Like

Well… I just tested and it worked!
Running my first .NET Standard (real) code! :slight_smile:
I think I get it now, and I like it better than PCL :slight_smile:

Now, now… if it tries to run on a platform where no platform specific assembly has been provided, it will run the plain .NET Standard version, which can fallback to a default implementation or throw some exception of choice (i.e. the infamous “PlatformNotSupportedException” which happen already today with some default libraries)

1 Like


Can you give some best practice full and detailed example for new Bait and Switch Trick with new Net Standard library and new nuget package and project files for visual studio 2019?


.NET Foundation Website | Blog | Projects | Code of Conduct