Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问属性属性的较短方式,无需访问两次_C#_.net_.net Core - Fatal编程技术网

C# 访问属性属性的较短方式,无需访问两次

C# 访问属性属性的较短方式,无需访问两次,c#,.net,.net-core,C#,.net,.net Core,首先执行包含名称和命令的Select。然后按名字点菜 从这里开始,只需根据需要访问成员即可 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class BotCategoryAttribute : Attribute { public string Name { get; set; } public BotCategoryAttribute(string name) {

首先执行包含名称和命令的Select。然后按名字点菜

从这里开始,只需根据需要访问成员即可

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class BotCategoryAttribute : Attribute 
{
    public string Name { get; set; }

    public BotCategoryAttribute(string name)
    {
        Name = name;
    }
}
首先执行包含名称和命令的Select。然后按名字点菜

从这里开始,只需根据需要访问成员即可

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class BotCategoryAttribute : Attribute 
{
    public string Name { get; set; }

    public BotCategoryAttribute(string name)
    {
        Name = name;
    }
}

首先执行包含名称和命令的select。然后按名称排序。首先选择包含名称和命令的选项。然后按名字点菜。
var orderedList = subcommands.Select(s => new { 
    SubCommand = s,
    Name = (Attribute.GetCustomAttribute(s.Module.ModuleType, typeof(BotCategoryAttribute)) as BotCategoryAttribute)?.Name
}).OrderBy(s => s.Name);

foreach (var cmd in orderedList) {
    string moduleName = cmd.Name;
    if (moduleName != null) {
        if (!modules.ContainsKey(moduleName)) {
            modules[moduleName] = new List<Command>();
        }    
        modules[moduleName].Add(cmd.SubCommand);
    }
}