基于属性对枚举执行 LINQ 操作
我正在尝试根据每个枚举选项的属性查询我的枚举。
我知道如何获取列表。通过此代码,这非常简单:
var list = Enum.GetValues(typeof(FamilyNameOptions))
.Cast<FamilyNameOptions>()
.Select(v => v.ToString())
.ToList();
如果这是我的枚举设置,我如何查询值为 TRUE 的属性 DrawingListIsEnabled
public enum FamilyNameOptions
{
[DrawingListIsEnabled(true)]
[FamilyUserName("FamilyName1")]
FamilyName1= 0,
[DrawingListIsEnabled(false)]
[FamilyUserName("FamilyName2")]
FamilyName2= 1,
}
/// <summary>
/// DrawingListIsEnabledAttribute
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.All)]
public class DrawingListIsEnabledAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DrawingListIsEnabledAttribute"/> class.
/// </summary>
/// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
public DrawingListIsEnabledAttribute(bool isEnabled)
{
this.IsEnabled = isEnabled;
}
/// <summary>
/// Gets a value indicating whether this instance is enabled.
/// </summary>
/// <value>
/// <c>true</c> if this instance is enabled; otherwise, <c>false</c>.
/// </value>
public bool IsEnabled { get; private set; }
}
预期结果将是一个列表1:
姓氏1
I'm trying to query against my Enum based on an attribute on each enum option.
I know how to get the list. That is pretty simple via this code:
var list = Enum.GetValues(typeof(FamilyNameOptions))
.Cast<FamilyNameOptions>()
.Select(v => v.ToString())
.ToList();
If this is my Enum setup, how can I query against the attribute DrawingListIsEnabled where the value is TRUE
public enum FamilyNameOptions
{
[DrawingListIsEnabled(true)]
[FamilyUserName("FamilyName1")]
FamilyName1= 0,
[DrawingListIsEnabled(false)]
[FamilyUserName("FamilyName2")]
FamilyName2= 1,
}
/// <summary>
/// DrawingListIsEnabledAttribute
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.All)]
public class DrawingListIsEnabledAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DrawingListIsEnabledAttribute"/> class.
/// </summary>
/// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
public DrawingListIsEnabledAttribute(bool isEnabled)
{
this.IsEnabled = isEnabled;
}
/// <summary>
/// Gets a value indicating whether this instance is enabled.
/// </summary>
/// <value>
/// <c>true</c> if this instance is enabled; otherwise, <c>false</c>.
/// </value>
public bool IsEnabled { get; private set; }
}
The expected result would be a list of 1:
FamilyName1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用反射来查找静态字段列表,而不是使用
Enum.GetValues
;由于这些信息在运行时都不会更改,因此您可能希望创建一个辅助类型来缓存结果。
Rather than using
Enum.GetValues
you will need to use reflection to find the static field list;Since none of that information will change at runtime, you might wish to create a helper type to cache the results.
下面是完成此任务的简单 LINQ 查询:
注意,您不需要原始的
列表
。另外,我使用的是Enum.GetValues
的通用版本,这消除了您的版本中对Cast
的需要。为了自我记录,我保留了 LINQ 名称很长时间;请随意使用典型的较短名称。该代码的工作原理如下:
Enum.GetValues
返回enum FamilyNameOptions
成员的强类型列表。.Select
语句获取描述枚举成员(以及所有自定义属性)的MemberInfo
对象,.Where
根据 < code>DrawingListIsEnabledAttribute 的IsEnabled
属性.Select
从FamilyUserNameAttribute
中获取名称FamilyUserName
属性(我想这就是它的名字 - 如果不是,请相应地更改它)。Here's a simple LINQ query to accomplish this task:
Note, you don't need your original
list
. Also, I'm using the generic version ofEnum.GetValues<TEnum>
, which eliminates the need forCast
in your version.I kept my LINQ names long in effort to be self-documenting; feel free to go with the typical shorter names. The code works as follows:
Enum.GetValues<FamilyNameOptions>
returns a strongly-typed list of members ofenum FamilyNameOptions
..Select
statement gets theMemberInfo
objects describing the enum members (along with all custom attributes).Where
filters the results based onDrawingListIsEnabledAttribute
'sIsEnabled
property.Select
grabs the names from theFamilyUserNameAttribute
'sFamilyUserName
property (I presume that's what it's called - change it accordingly if not).