SYSLIB0036: Regex.CompileToAssembly is obsolete

The Regex.CompileToAssembly method is marked as obsolete, starting in .NET 7. Using this API in code generates warning SYSLIB0036 at compile time.

In .NET 5, .NET 6, and all versions of .NET Core, Regex.CompileToAssembly throws a PlatformNotSupportedException. In .NET Framework, Regex.CompileToAssembly allows a regular expression instance to be compiled into an assembly.

Workaround

Use the GeneratedRegexAttribute attribute, which invokes a regular expression source generator. At compile time, the source generator produces an API specific to a regular expression pattern and its options.

// This attribute causes the regular expression pattern to be compiled into your assembly,
// which enables it to start up and run more quickly.
[GeneratedRegex("abc|def", RegexOptions.IgnoreCase)]
private static partial Regex MyRegex();

// ...

// Use the regular expression
if (MyRegex().IsMatch(text) { ... }

Suppress a warning

If you must use the obsolete APIs, you can suppress the warning in code or in your project file.

To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.

// Disable the warning.
#pragma warning disable SYSLIB0036

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0036

To suppress all the SYSLIB0036 warnings in your project, add a <NoWarn> property to your project file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0036</NoWarn>
  </PropertyGroup>
</Project>

For more information, see Suppress warnings.