审查生成的用户资料

来自 属性
C# 14 / .NET 10 进阶 6分钟 找出 4处问题

在这个生成的资料模型用于控制应用角色前,对它进行审查。

保存非空白名称与 slug,由类型控制角色变更,并让重复读取简介时使用内存状态。

csharp
using System;
using System.Collections.Generic;
using System.IO;

public sealed class UserProfile
{
    public required string Name { get; set; }
    public List<string> Roles { get; init; } = [];

    private string _slug = string.Empty;
    public string Slug
    {
        get => _slug;
        set
        {
            _slug = value.Trim();
            if (_slug.Length == 0)
                throw new ArgumentException("Slug is required");
        }
    }

    public string Biography => File.ReadAllText("bio.txt");
    public bool IsAdmin => Roles.Contains("admin");
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误