选择具有特定属性值的属性
问题描述:
我正在寻找一种方法来选择在单个LINQ语句中具有特定值的特定自定义属性的属性。选择具有特定属性值的属性
我得到的属性,我想要的属性,但我不知道如何选择其特定的值。
<AttributeUsage(AttributeTargets.Property)>
Public Class PropertyIsMailHeaderParamAttribute
Inherits System.Attribute
Public Property HeaderAttribute As String = Nothing
Public Property Type As ParamType = Nothing
Public Sub New()
End Sub
Public Sub New(ByVal headerAttribute As String, ByVal type As ParamType)
Me.HeaderAttribute = headerAttribute
Me.Type = type
End Sub
Public Enum ParamType
base = 1
open
closed
End Enum
End Class
private MsgData setBaseProperties(MimeMessage mailItem, string fileName)
{
var msgData = new MsgData();
Type type = msgData.GetType();
var props = from p in this.GetType().GetProperties()
let attr = p.GetCustomAttributes(typeof(Business.IT._21c.AddonFW.PropertyIsMailHeaderAttribute), true)
where attr.Length == 1
select new { Property = p, Attribute = attr.FirstOrDefault() as Business.IT._21c.AddonFW.PropertyIsMailHeaderAttribute };
}
[解决方法]
var baseProps = from p in this.GetType().GetProperties()
let attr = p.GetCustomAttribute<PropertyIsMailHeaderParamAttribute>()
where attr != null && attr.Type == [email protected]
select new { Property = p, Attribute = attr as Business.IT._21c.AddonFW.PropertyIsMailHeaderParamAttribute };
答
您可能已投下Attribute
对象(使用使用OfType<>
扩展名的常规演员或通过例如)你的类型,但最简单的方法是使用通用版本GetCustomAttribute<>
:
var props = from p in this.GetType().GetProperties()
let attr = p.GetCustomAttribute<PropertyIsMailHeaderAttribute>()
where attr != null && attr.HeaderAttribute == "FooBar"
&& attr.Type = ParamType.open
select whatever;
+0
谢谢你。这帮了我很多: – OhSnap
是不是第一类的代码,VB.NET中的PropertyIsMailHeaderParamAttribute? –
是的,我们的库是用VB.NET编写的,这是我从中获得CustomAttribute的地方。不要问为什么..犯了错误。如果你们想知道的话,我也改变了这些类的名字。 – OhSnap