---
source_url: https://itmustbecode.com/dataverse-plugins-unlock-the-latest-c-features-with-polysharp/
title: Dataverse Plugins : Unlock the latest C# features with PolySharp
date: 2023-01-20T03:30:30+00:00
categories:
  - blog
tags:
  - custom-api
  - dataverse
  - plugin
  - polysharp
word_count: 1039
reading_time_minutes: 5
type: posts
---

Whenever a new version of C# is released I'm always eager to explore the new features of the language. However, as a Power Platform developer, it can be frustrating knowing that they can't be used to develop Dataverse plugins, which constitutes a significant part of my daily work.

As a matter of fact, due to a dependency on the Microsoft CRM SDK, Dataverse plugins are confined to use an older version of the .NET framework (4.6.2) which natively supports C#7.3, while the latest version is C# 11.

Well, those days are over! In this post we'll see how to take Dataverse plugin development to the next level and unlock the latest C# features with the help of the nifty **[PolySharp](https://github.com/Sergio0694/PolySharp)** library created by [Sergio Pedri](https://twitter.com/SergioPedri) from Microsoft.

## PolySharp to the rescue

The motivation behind **PolySharp** is to adress this very issue. It allows developers to enable the latest version of C# compiler while targeting older versions of the .NET framework which would normally be incompatible.

It does it's magic by filling the gaps that makes the compiler complain when using modern syntax. Using **source generators**, PolySharp detects the missing types required for the features that are not implemented in the (older) target framework and inject the appropriate **polyfills**.

Fun fact, the library is already being used internally at Microsoft with positive impact, which gives me confidence on its future.

{{< figure src="./image-31.png" alt="" caption="" >}}

## Configure your project to use PolySharp

Let's start from the ground up and see how easy it is to configure a **Dataverse Plugin** project that target C#11 and leverages PolySharp.


> To follow along, you can find the sample project I used for the blog post in this [github repo](https://github.com/drivardxrm/Dataverse.Polysharp.Plugin).


### **(Pre-requisite) Enable PackageReference mode**

To be compatible with PolySharp it's **mandatory** to set the Package Management mode of the project to **PackageReference**. Ensure that your Visual Studio settings is correctly set here.

**Tools**-\> **NuGet Package Manager** -\> **Package Manager Settings**

{{< figure src="./image-32.png" alt="" caption="" >}}


> If you start from a project is already in packages.config mode, you can easily migrate. See the official documentation [here](https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference)


### **1- Create a Dataverse plugin project**

To begin we need to a class library project that targets the .NET Framework 4.6.2. Easiest way is to fire up Visual Studio and and create a new project using the **Class Library (.NET Framework)** template.

{{< figure src="./image-25.png" alt="" caption="" >}}

{{< figure src="./image-26.png" alt="" caption="" >}}

### **2- Install Dependencies**

Now, install the required nuget packages, at the minimum install these 2 packages.

- [Microsoft.CrmSdk.CoreAssemblies](https://www.nuget.org/packages/Microsoft.CrmSdk.CoreAssemblies/)
- [PolySharp](https://www.nuget.org/packages/PolySharp)

{{< figure src="./image-21.png" alt="" caption="" >}}

At this point, the PolySharp source generator should be visible in the **Analyzers** node of the projects **References** section. We are almost there ...

{{< figure src="./image-29.png" alt="" caption="" >}}

### **3- Bump the Language version in the csproj file**

Now, to ensure that the project is compiled using the **latest** version of C#, navigate to the project's **csproj** file and insert the following line of code within the first **PropertyGroup** node.

```
<LangVersion>11.0</LangVersion>
```

{{< figure src="./image-28.png" alt="" caption="" >}}

That's all there is in terms of configuration. From now on, since the project compiles C#11, the latest C# syntax can be used and Intellisense will pick up as well. Thanks to the **PolySharp** source generator the gaps left by the older framework will be patched with the appropriate polyfills... it's a pure gem 💎.

## Dataverse Plugin Using C\#11

To illustrate this in the context of a Dataverse plugin ([link to the repo](https://github.com/drivardxrm/Dataverse.Polysharp.Plugin)), I will implement a Dataverse Custom API to play RockPaperScissor (✊🖐️✌️) using some of the latest C# features. _don't take this too seriously there's better ways to code this , I just want to showcase the new syntax._

The Custom API takes the name of the player and the hand (rock, paper or scissor) as input and send back the result of a game against the computer. The Custom API will execute the plugin **Dataverse.Polysharp.Plugin.RockPaperScissor** from the project (see code below)


> Screenshot taken from my [Custom API Manager](https://itmustbecode.com/customapi-manager-for-xrmtoolbox/) tool for XrmToolbox.  Shameless plug 🔌


{{< figure src="./image-30.png" alt="" caption="" >}}

By looking at the plugin code, you can spot at least 5 usages of modern C# syntax that are natively unavailable to .NET Framework 4.6.2, yet everything **compiles flawlessly**.

1. **Global usings** : it really helps to de-clutter the top of each files
1. **File-scoped namespace** : reduces the nesting of the file and give back some real estate
1. **Record**: usage of the new record struct with init-only properties
1. **Raw string litterals** : makes it easier to deal with multiline strings that contains brackets and quotes. Very useful to write json strings
1. **Pattern matching** and **modern switch** statement

Once the assembly is compiled and deployed to the Dataverse environment, the **RockPaperScissor** custom API can be invoked and the business logic is executed.


> To test the API, I'm using the [Custom API Tester](https://www.xrmtoolbox.com/plugins/Rappen.XrmToolBox.CustomAPITester/) XrmToolbox tool from Jonas Rapp


{{< figure src="./rockpaperscissor-min.gif" alt="" caption="" >}}

How cool is that! We just executed a C#11-enabled Dataverse Plugin🤯

## TakeAway

For me, this is a real game changer and I can't wait to step out of the dark ages and test **PolySharp** on my real Dataverse plugin codebases. I see a lot of potential not only for plugins but also for **XrmToolbox** tool authoring.

Dont forget to star⭐ the [PolySharp](https://github.com/Sergio0694/PolySharp) project on Github if you like it!

I also recommend this video by [Nick Chapsas](https://twitter.com/nickchapsas) that test drives the library.
{{< linkcard url="https://www.youtube.com/watch?v=RgKa-tjnUMA" title="watch?v=RgKa-tjnUMA" summary="" image="/linkcards/1a02b2f76aaf92ce.jpg" domain="www.youtube.com" new_tab="true" nofollow="true" >}}

## Links


{{< linkcard url="https://github.com/Sergio0694/PolySharp" title="GitHub - Sergio0694/PolySharp: PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C# version to latest and have fun! 🚀" summary="PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C# version to latest and have fun! 🚀 -..." image="/linkcards/fb35d300cdf896f7.png" domain="github.com" new_tab="true" nofollow="true" >}}


{{< linkcard url="https://github.com/drivardxrm/Dataverse.Polysharp.Plugin" title="GitHub - drivardxrm/Dataverse.Polysharp.Plugin: Testing the Poly# library with on a Dataverse plugin project" summary="Testing the Poly# library with on a Dataverse plugin project - GitHub - drivardxrm/Dataverse.Polysharp.Plugin: Testing the Poly# library with on a Dataverse plugin project" image="/linkcards/aaaf48348b0fe266.png" domain="github.com" new_tab="true" nofollow="true" >}}


{{< linkcard url="https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference?WT.mc_id=DX-MVP-5004959" title="Migrating from packages.config to PackageReference formats" summary="Details on how to migrate a project from the packages.config management format to PackageReference as supported by NuGet 4.0+ and VS2017 and .NET Core 2.0" image="/linkcards/f69ad1e3eed32c06.png" domain="learn.microsoft.com" new_tab="true" nofollow="true" >}}

{{< linkcard url="/customapi-manager-for-xrmtoolbox/" title="Custom API Manager for XrmToolBox" summary="Like most Power Platform developers, I am a heavy user of the XrmToolBox. This is the story of creating a tool to contribute to the community." image="./2021-01-03_19-53-32.png" domain="itmustbecode.com" new_tab="false" nofollow="false" >}}


{{< linkcard url="https://www.xrmtoolbox.com/plugins/Rappen.XrmToolBox.CustomAPITester/" title="Custom API Tester · XrmToolBox" summary="Browse Custom APIs, enter input parameters, execute the action, investigate output parameters." image="/linkcards/7963c972123cfedd.png" domain="www.xrmtoolbox.com" new_tab="true" nofollow="true" >}}

Image by [PDPics](https://pixabay.com/users/pdpics-44804/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=166882) from [Pixabay](https://pixabay.com//?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=166882)

