审查生成的命令目录

来自 特性
C# 14 / .NET 10 高级 6分钟 找出 4处问题

在这个生成的命令目录扫描应用程序集前,对它进行审查。

在启动时构建一次目录,保留所有直接与继承别名,安全处理未标记类型,并让发现过程没有外部副作用。

csharp
using System;
using System.IO;
using System.Linq;
using System.Reflection;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class CommandAttribute : Attribute
{
    public string Name { get; }
    public CommandAttribute(string name)
    {
        File.AppendAllText("command-discovery.log", name + Environment.NewLine);
        Name = name;
    }
}

public static class CommandCatalog
{
    public static string[] Build(Type[] handlerTypes)
    {
        return handlerTypes.Select(type =>
            type.GetCustomAttribute<CommandAttribute>(inherit: false)!.Name).ToArray();
    }
}

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

在试验场中打开
报告错误