|

JsonNode – .NET 8 API updates

Part of .NET 8, JsonNode and JsonArray get a few new additions to their API. We’ve already covered deep copy and deep equality in an earlier article, but there’s more. GetValueKind Returns the JsonValueKind of the current instance. GetPropertyName Returns property name of the current node from the parent object. Throws an InvalidOperationException if the…

|

System.Text.Json – Disable reflection-based serialization

Starting with .NET 8 you can disable the default reflection-based serializer that comes with System.Text.Json. This can be useful in trimmed and native AOT applications where you don’t want to include the reflection components in your build. You can enable this feature by setting the JsonSerializerIsReflectionEnabledByDefault property to false in your .csproj file. As a…

|

Add/Remove TypeInfoResolver to existing JsonSerializerOptions

Starting with .NET 8, the JsonSerializerOptions class features a new TypeInfoResolverChain property in addition to the existing TypeInfoResolver property. With this new property, you are no longer required to specify all the resolvers in the same place, instead, you can add them later as needed. Let’s look at an example: Besides adding new type resolvers…

| |

WPF – Prevent file dialog selection from being added to recents

Files opened or saved using WPF’s file dialogs (OpenFileDialog, SaveFileDialog or OpenFolderDialog) are by default added to the Windows Explorer’s recent files list and can also impact the Recommended section of the Start Menu in Windows 11. To disable this behavior, you can set AddToRecent to false on your dialog before calling the ShowDialog() method….

| |

WPF – Individual dialog states using ClientGuid

A new ClientGuid property, introduced with .NET 8, allows you to uniquely identify dialogs such as the OpenFileDialog and the OpenFolderDialog, with the purpose of storing state – such as window size, position and last used folder – separately per dialog. To benefit from this behavior, configure the ClientGuid of your dialog to a known…

| |

WPF – Limit OpenFileDialog folder tree to a certain folder

Starting with .NET 8, you can constrain the OpenFileDialog and OpenFolderDialog folder tree to a given root folder. You can do so by setting the RootDirectory property on the dialog before calling ShowDialog(). It’s very important to note that this does not limit the selection and the navbar navigation in any way. The user will…

| |

WPF Open Folder Dialog

.NET 8 introduces a new OpenFolderDialog to Windows Presentation Foundation (WPF). This enables application users to browse and select one or multiple folders. Usage is simple: create a new OpenFolderDialog, provide a Title, and an InitialDirectory. And if you want to allow your users to select multiple folders, set Multiselect to true. Next, a simple…

| |

WPF hardware acceleration in RDP

WPF applications use by default software rendering when accessed over remote desktop, even if the system has hardware rendering capabilities. With .NET 8, a new option is introduced which allows you to opt into hardware acceleration when using the Remote Desktop Protocol. This can result in improved performance and an overall more responsive application. You…

| |

NET 8 – Serializing properties from interface hierarchies

.NET 8 adds support for serializing properties from interface hierarchies. This means that all the properties from all interfaces in the hierarchy will be included in the serialization. The most important thing is where you start. Let’s take the following hierarchy as an example: Now, during serialization, if you pass along a Derived2Impl instance stored…

| |

NET 8 – Deserialize into non-public properties

Similar to serializing into non-public members, you can deserialize into non-public members by providing a constructor with parameters matching the non-public member names and by annotating the non-public members with the JsonInclude attribute. Let’s jump straight to an example: Note how we haven’t annotated PublicProperty in any way and we haven’t included it in the…

| |

NET 8 – How to use JsonStringEnumConverter with native AOT

JsonStringEnumConverter is not compatible with native AOT. To fix that, NET 8 introduces a new converter type JsonStringEnumConverter<TEnum> that is compatible with native AOT. To use the new type, simply annotate your types as follows: Keep in mind: enum deserialization is case insensitive, while serialization can be cusomized via JsonNamingPolicy. What happens if you try…