我有以下示例:
public class Commands
{
public int ID { get; set; }
public List<string> Alias { get; set; }
}
public class UserAccess
{
public int AccessID { get; set; }
// other stuff not needed for the question
public List<Commands> AllowedCommands { get; set; }
}
现在我想在 UserAccess 上实现一种返回命令 ID 或 NULL 的方法,如果在列表中没有找到别名,请看下面我所说的一个肮脏的例子 HasCommand
:
public class UserAccess
{
public ID { get; set; }
// other stuff not needed for the question
public List<Commands> AllowedCommands { get; set; }
public Commands HasCommand(string cmd)
{
foreach (Commands item in this.AllowedCommands)
{
if (item.Alias.Find(x => string.Equals(x, cmd, StringComparison.OrdinalIgnoreCase)) != null)
return item;
}
return null;
}
}
我的问题是运行或实现 HasCommand 方法的最有效方法是什么?
还是有更好的方法将其实施到 UserAccess 中?