microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support code generation targeting C# < 9

AArnott opened this issue · comments

Source generators work in projects that target less than the C# 9 language version, so the generator should take the LangVersion into account and generate valid code given that target version.

Examples of C# features the generated code may rely on that require newer language versions include:

  1. Nullable ref annotations (C# 8)
  2. Function pointers (C# 9)
  3. 'null pointer constant pattern':

    Windows.Win32.PCWSTR.g.cs(52,15,52,19): error CS8370: Feature 'null pointer constant pattern' is not available in C# 7.3. Please use language version 8.0 or greater.

  4. 'readonly members'

    Windows.Win32.D2D_MATRIX_4X4_F.g.cs(66,22,66,30): error CS8370: Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Note that C# 9 does work on projects that target .NET Framework as well as .NET Core 3.1. But there are features of C# 8+ that do not work on .NET Framework, such as default interface methods from C# 8 or certain calling conventions of function pointers. By avoiding use of such features, many projects have successfully targeted .NET Framework while compiling with the C# 9 language version.

This becomes less of a priority as time goes by, and it's already so low relative to other issues we won't likely get to it while there is any interest.