FlexibleRequired.Analyzers 1.0.0

dotnet add package FlexibleRequired.Analyzers --version 1.0.0
                    
NuGet\Install-Package FlexibleRequired.Analyzers -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="FlexibleRequired.Analyzers" 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.
<PackageVersion Include="FlexibleRequired.Analyzers" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FlexibleRequired.Analyzers">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FlexibleRequired.Analyzers --version 1.0.0
                    
#r "nuget: FlexibleRequired.Analyzers, 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.
#:package FlexibleRequired.Analyzers@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FlexibleRequired.Analyzers&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FlexibleRequired.Analyzers&version=1.0.0
                    
Install as a Cake Tool

FlexibleRequired 示例

本项目展示了 FlexibleRequired 分析器和特性系统的各种使用场景。

运行示例

cd FlexibleRequired.Sample
dotnet run

示例分类

1. 基础用法示例

  • Person 类: 展示属性上的基本 [Required] 特性
  • 演示带有必需属性的有效对象初始化
  • 展示可选属性可以被省略

2. 继承示例

  • Employee → Manager: 展示派生类如何将可选属性变为必需
  • Employee → Contractor: 展示派生类如何使用 [Required(false)] 放宽要求
  • 演示具有不同要求级别的继承层次结构

3. 不同数据类型示例

  • Product 类: 展示各种数据类型上的 [Required] 特性:
    • 基本类型 (string, decimal)
    • 集合类型 (List<string>, Dictionary<string, string>)
    • 带有默认值的可选属性

4. 真实世界场景

  • ApiRequest: 带有一些自动生成属性的 API 请求模型
  • DatabaseConfig: 带有必需和可选设置的配置对象
  • UserPreferences → AdminPreferences: 基于角色要求的用户设置

5. OptionalRequired 构造函数示例

  • User 类: 展示构造函数上的 [OptionalRequired] 特性来有选择地使属性变为可选
  • ServerConfig 类: 演示具有不同可选成员配置的多个构造函数
  • 展示构造函数级别的特性如何覆盖属性级别的 [Required] 特性

主要功能演示

必需特性

[Required]
public string Name { get; init; } = "";

可选属性

// 没有 [Required] 特性意味着可选
public int? Age { get; init; }

继承覆盖

// 在派生类中使属性变为可选
[Required(false)]
public override string EmployeeId { get; init; } = "";

// 在派生类中使可选属性变为必需
[Required]
public new decimal? Salary { get; init; }

默认值

// 带有默认值的属性
public bool InStock { get; init; } = true;
public DateTime Timestamp { get; init; } = DateTime.UtcNow;

OptionalRequired 构造函数

public class User
{
    [Required]
    public string Username { get; init; } = "";
    
    [Required]
    public string Email { get; init; } = "";
    
    [Required]
    public string DisplayName { get; init; } = "";
    
    [Required]
    public string Bio { get; init; } = "";

    // 使 DisplayName 和 Bio 变为可选的构造函数
    [OptionalRequired("DisplayName", "Bio")]
    public User(string username, string email)
    {
        Username = username;
        Email = email;
        DisplayName = username; // 默认值
        Bio = ""; // 默认值
    }

    // 具有不同可选要求的构造函数
    [OptionalRequired("DisplayName")]
    public User(string username, string email, bool isPremium)
    {
        Username = username;
        Email = email;
        DisplayName = username;
        IsPremium = isPremium;
        // 使用此构造函数时 Bio 仍然是必需的
    }
}

错误示例(已注释)

文件中包含已注释的错误示例,当 FlexibleRequired 分析器激活时会触发编译错误:

  • 缺少必需属性
  • 不完整的对象初始化
  • 继承要求违规

要查看这些错误的实际效果,请取消注释错误示例部分并尝试构建项目。

分析器行为

当启用 FlexibleRequired 分析器时,它将:

  1. 分析对象初始化器 以确保所有 [Required] 属性都已设置
  2. 遵循继承层次结构 和属性覆盖
  3. 处理 OptionalRequired 构造函数 - 当使用带有 [OptionalRequired] 的构造函数时,指定的成员对该初始化变为可选
  4. 验证 OptionalRequired 特性 - 确保 [OptionalRequired] 中指定的成员名称确实存在于类中
  5. 提供有用的错误消息 指向缺少的必需属性
  6. 支持复杂场景 如集合、泛型和嵌套对象

OptionalRequired 验证

分析器包含三个诊断规则:

  • RMQ001: 对象初始化期间缺少必需成员
  • RMQ002: [OptionalRequired] 特性中的无效成员名称(当指定的成员不存在时发出警告)
  • RMQ003: [OptionalRequired] 特性中的冗余成员(当成员已在构造函数中赋值时发出提示)

构建和测试

示例项目引用:

  • FlexibleRequired(特性库)
  • FlexibleRequired.Analyzers(Roslyn 分析器)

当你构建此项目时,分析器将自动验证你的对象初始化并将任何缺少的必需属性报告为编译错误。

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 253 6/13/2025