Required 1.0.0

dotnet add package Required --version 1.0.0                
NuGet\Install-Package Required -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Required" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Required --version 1.0.0                
#r "nuget: Required, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Required as a Cake Addin
#addin nuget:?package=Required&version=1.0.0

// Install Required as a Cake Tool
#tool nuget:?package=Required&version=1.0.0                

Required Nuget

Use C# 11's required member feature in in older target frameworks.

:running: Quickstart   |   :package: NuGet


You may also want to check out Manuel Römer's Nullable project which provides support for .NET's nullable reference type attributes for older target frameworks.


C# 11 added support for the required keyword. When using C# 11 with target frameworks ⇐ .NET 7.0, using this new feature is not possible because the compiler is missing the RequiredMember and CompilerFeatureRequired attributes, hence making the features unavailable for any target framework prior to .NET 7.

Luckily, this problem can be solved by re-declaring the attributes as an internal class in your own project. The compiler will use these custom class definitions and thus allow you to use the required keyword in any project.

This repository hosts the code for the "Required" NuGet Package which, when referenced, automatically adds the attributes to the referenced project(s).

The code for the Required class is added at compile time and gets built into the referencing project. This means that the resulting project does not have an explicit dependency on the Required package, because the code is not distributed as a standard library.

Compatibility

Required is currently compatible with the following target frameworks:

  • .NET Standard >= 2.0

Quickstart

⚠️ Important:
You must use a C# version >= 11.0 with the Required package - otherwise, your project won't compile. The steps below assume that you are using the new SDK .csproj style. Please find installation guides and notes for other project types (for example packages.config) here.

  1. Reference the package

    Add the package to your project, for example via:

    Install-Package Required
    --or--
    dotnet add package Required
    
  2. Ensure that the package has been added as a development dependency
    Open your .csproj file and ensure that the new package reference looks similar to this:

    <PackageReference Include="Required" Version="<YOUR_VERSION>" PrivateAssets="all" />
    
    
    <PackageReference Include="Required" Version="<YOUR_VERSION>">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    

    This is especially important for libraries that are published to NuGet, because without this, the library will have an explicit dependency on the Required package.

  3. Build the project
    Ensure that the project compiles. If a build error occurs, you will most likely have to update the C# language version.

Afterwards, you can immediately start using the attributes.

Compiler Constants

The included C# file makes use of some compiler constants that can be used to enable or disable certain features.

REQUIRED_DISABLE

If the REQUIRED_DISABLE constant is defined, the attributes needed to support the feature are excluded from the build. This can be used to conditionally exclude code of this package from the build if it is not required.

In most cases, this should not be required, because the package automatically excludes the code from target frameworks that already support the feature.

REQUIRED_INCLUDE_IN_CODE_COVERAGE

Because the attributes are added as source code, it could appear in code coverage reports. By default, this is disabled via the ExcludeFromCodeCoverage and DebuggerNonUserCode attributes.

By defining the REQUIRED_INCLUDE_IN_CODE_COVERAGE constant, the ExcludeFromCodeCoverage and DebuggerNonUserCode attributes are not applied and the attributes may therefore appear in code coverage reports.

Building

Because the package consists of source files, building works differently than a normal .NET project. In essence, no build has to be made at all. Instead, the *.cs files are packaged into a NuGet package through custom packaging targets.

Contributing

Open to any contributions!

License

See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Required:

Repository Stars
TelegramBots/Telegram.Bot
.NET Client for Telegram Bot API
soxtoby/SlackNet
A comprehensive Slack API client for .NET
Version Downloads Last updated
1.0.0 15,227 7/13/2022