Home > Enterprise >  How to check in c# code if the current project is xamarin or not?
How to check in c# code if the current project is xamarin or not?

Time:12-20

I am looking for a preprocessor symbol that would allow me to compile different code based on whether a project is xamarin or not.

void a()
{
#if XAMARIN
    b();
#else
    c();
#endif
}

CodePudding user response:

I would suggest you don't use preprocessor symbols, but abstract stuff in separate classes for each platform. Then inject the specific implementation at runtime.

If you really must use these, you can always define your own symbols as needed for each configuration or target framework.

Just create a file called Directory.Build.targets (casing matters!) in the root of your repo. Usually next to your .sln file.

In this Directory.Build.targets you can define symbols like so:

<Project>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('netstandard'))">
    <DefineConstants>$(DefineConstants);NETSTANDARD;PORTABLE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
    <DefineConstants>$(DefineConstants);NET;WPF;XAML</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('uap'))">
    <DefineConstants>$(DefineConstants);NETFX_CORE;XAML;WINDOWS;WINDOWS_UWP;UWP</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
    <DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA;APPLE;IOS</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.mac'))">
    <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;MAC</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.tvos'))">
    <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;TVOS</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.watchos'))">
    <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;WATCHOS</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('monoandroid'))">
    <DefineConstants>$(DefineConstants);MONO;ANDROID</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(TargetFramework)' == 'monoandroid10.0'">
    <DefineConstants>$(DefineConstants);MONO;ANDROID;__ANDROID_29__;</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
    <DefineConstants>$(DefineConstants);NETCORE;NETCOREAPP</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp3.'))">
    <DefineConstants>$(DefineConstants);WPF</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('tizen'))">
    <DefineConstants>$(DefineConstants);TIZEN</DefineConstants>
  </PropertyGroup>
</Project>

Then you can use these in your project as needed:

#if PORTABLE
   // do portable stuff
#elif UWP
   // do uwp stuff
#endif
  • Related