c# 4.0 获取类属性(指定的属性和attribute.data)
我想知道我的类的哪些属性具有带有具体字符串的确切属性。我有这个实现(属性和类):
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class IsDbMandatory : System.Attribute
{
public readonly string tableField;
public IsDbMandatory (string tableField)
{
this.tableField= TableField;
}
}
public Class MyClass
{
[IsDbMandatory("ID")]
public int MyID { get; set; }
}
然后我以这种方式获取具有具体属性的属性:
public class MyService
{
public bool MyMethod(Type theType, string myAttributeValue)
{
PropertyInfo props =
theType.GetProperties().
Where(prop => Attribute.IsDefined(prop, typeof(IsDbMandatory)));
}
}
但我只需要具有具体属性 isDbMandatory
和具体字符串 myAttributeValue
里面。
我该怎么办?
I would like to get what properties of my class have an exact attribute with a concrete string. I have this implementation (Attribute and Class):
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class IsDbMandatory : System.Attribute
{
public readonly string tableField;
public IsDbMandatory (string tableField)
{
this.tableField= TableField;
}
}
public Class MyClass
{
[IsDbMandatory("ID")]
public int MyID { get; set; }
}
Then I get the property with the concrete attribute in this way:
public class MyService
{
public bool MyMethod(Type theType, string myAttributeValue)
{
PropertyInfo props =
theType.GetProperties().
Where(prop => Attribute.IsDefined(prop, typeof(IsDbMandatory)));
}
}
But I need only the properties with concrete attribute isDbMandatory
AND concrete string myAttributeValue
inside.
How can I do it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)