empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
531 stars 132 forks source link

Init-only properties cannot be used directly from .NET Framework projects #180

Closed qacha closed 1 month ago

qacha commented 1 month ago

In v6.2.0-preview-1, PdfSharp.Pdf.Signatures.DigitalSignatureOptions is implemented using init-only properties. Trying to use it from a .NET Framework project:

var options = new DigitalSignatureOptions { ContactInfo = "value" };

Results in this error: error CS8370: Feature 'init-only setters' is not available in C# 7.3. Please use language version 9.0 or greater.

... but C# 9.0 requires .NET 5.0+.

It's possible to work around this using reflection:

var options = new DigitalSignatureOptions(); typeof(DigitalSignatureOptions).GetProperty(nameof(DigitalSignatureOptions.ContactInfo)).SetValue(options, "value");

But this clearly not ideal and the init-only properties don't seem to have any significant advantages in this case.

ThomasHoevel commented 1 month ago

AIUI not all C# 9.0 features require .NET 5.0+. PDFsharp/MigraDoc compile against .NET 4.6.2.

You can set the language level to C# 9.0 (or higher) and use compiler features that do not rely on library support.

ThomasHoevel commented 1 month ago

There is a NuGet package you can use to make this work: https://github.com/manuelroemer/IsExternalInit

Or you can add this code to your project:

    #if !NET5_0_OR_GREATER
    namespace System.Runtime.CompilerServices
    {
        internal static class IsExternalInit { }
    }
    #endif