Platform specific code


#1

I wonder, hypothetically, how to write platform specific code in a .NET Core library.

For example let say I want to use .NET Core to develop Xamarin phone app (looks like it could be possible) and I want to write a class to display local notification. Obviously I would need some kind of OS stuff for iOS and Android. How would I proceed?

Could everything be in the same library?

Or would I need, 1 core common library one normal dotnet platform specific interface service implementation? How is it any better than PCL?


#2

The idea is that you wouldn’t write platform specific code. On .NET Core, it should run the same on any platform.

But, if you needed to, the best way would be to use dependency injection. Just Google the dependency injection pattern.


#3

Actually though, I wonder if you’ve got the terminology right…

.NET Core is a runtime environment. It’s not the the main runtime environment that you would use on say iOS or Android. These platforms use Mono - not .NET Core.

Are you perhaps referring to a .NET Standard library that can be run on all platforms?


#4

You might want to read this.

Hopefully you will get more insights to how things work and then you may figure things yourself.


#5

Nothing new here…

At work with Xamarin and PCL we started using similar patter. Each library comes as 2 DLLs (PCL + platform specific). The PCL dlls and a platform specific DLL that implement platform specific stuff.
I found that clunky.

Later I used bait and switch, shared project and #ifdef section. I like it much better. Each library comes as 1 platform specific DLL.

What I mean to say is I was hoping there was a way with .NET core to avoid pattern 1 (library being split into 2 DLL for each platform) but to use pattern 2, i.e. being able to ship each library as a single .NET Core platform specific DLL.

But as I described it that way I can feel it’s a doomed endeavor…

On the plus side we started to do our own Nuget package at work, and it quite simplify deployment of library which comes as multiple (platform dependent) DLLs. Just add 1 Nuget package, nugget take care of all the niggly little details for you…


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