Open sandboxFocus

Class UnionHelper

Recognizes a union of C# 15 at run time and reads its Value property, which holds the value of the case that the union currently carries.

Inheritance
UnionHelper
Namespace: Flashtrace.Formatters.Utilities
Assembly: Flashtrace.Formatters.dll
Syntax
public static class UnionHelper
Remarks

The compiler makes every union implement System.Runtime.CompilerServices.IUnion, which declares the Value property. That interface belongs to the base class library of .NET 11, while this assembly targets .NET Framework 4.7.2, .NET Standard 2.0 and .NET 10, so it cannot be named in source. The interface is therefore matched by its full name over the interfaces that the type implements, which gives the same answer when the .NET 10 assembly is loaded into a .NET 11 application.

A type of another assembly that declares an interface of the same full name is recognized as a union as well. That is the cost of matching by name, and the name belongs to a namespace that the base class library reserves for the compiler.

The results are cached per type, so a repeated query costs one dictionary lookup.

Methods

Name Description
IsUnion(Type)

Determines whether a type is a union.

TryGetValuePropertyGetter(Type, out Func<object, object?>?)

Gets a function that reads the Value property of a union.