C# 访问属性属性的较短方式,无需访问两次
首先执行包含名称和命令的Select。然后按名字点菜 从这里开始,只需根据需要访问成员即可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) {
[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);
}
}