来自 Properties 的 C# 自定义属性
所以我有一个来自我的类的属性集合,我想循环遍历它。 对于每个属性,我可能有自定义属性,因此我想循环遍历这些属性。 在这种特殊情况下,我的城市类上有一个自定义属性,
public class City
{
[ColumnName("OtroID")]
public int CityID { get; set; }
[Required(ErrorMessage = "Please Specify a City Name")]
public string CityName { get; set; }
}
属性是这样定义的
[AttributeUsage(AttributeTargets.All)]
public class ColumnName : System.Attribute
{
public readonly string ColumnMapName;
public ColumnName(string _ColumnName)
{
this.ColumnMapName= _ColumnName;
}
}
当我尝试循环属性[效果很好],然后循环属性时,它只会忽略属性的 for 循环,什么也不返回。
foreach (PropertyInfo Property in PropCollection)
//Loop through the collection of properties
//This is important as this is how we match columns and Properties
{
System.Attribute[] attrs =
System.Attribute.GetCustomAttributes(typeof(T));
foreach (System.Attribute attr in attrs)
{
if (attr is ColumnName)
{
ColumnName a = (ColumnName)attr;
var x = string.Format("{1} Maps to {0}",
Property.Name, a.ColumnMapName);
}
}
}
当我转到具有自定义属性的属性的直接窗口时,我可以这样做
?Property.GetCustomAttributes(true)[0]
它将返回 ColumnMapName: "OtroID"
虽然我似乎无法使其以编程方式工作
so I have a collection of Properties from my class which I want to loop through.
For each property I may have custom attributes so I want to loop through those.
In this particular case I have a custom attribute on my City Class as such
public class City
{
[ColumnName("OtroID")]
public int CityID { get; set; }
[Required(ErrorMessage = "Please Specify a City Name")]
public string CityName { get; set; }
}
The attribute is defined as such
[AttributeUsage(AttributeTargets.All)]
public class ColumnName : System.Attribute
{
public readonly string ColumnMapName;
public ColumnName(string _ColumnName)
{
this.ColumnMapName= _ColumnName;
}
}
When I try to loop through the properties [which works fine] and then loop through the attributes it just ignores the for loop for the attribute and returns nothing.
foreach (PropertyInfo Property in PropCollection)
//Loop through the collection of properties
//This is important as this is how we match columns and Properties
{
System.Attribute[] attrs =
System.Attribute.GetCustomAttributes(typeof(T));
foreach (System.Attribute attr in attrs)
{
if (attr is ColumnName)
{
ColumnName a = (ColumnName)attr;
var x = string.Format("{1} Maps to {0}",
Property.Name, a.ColumnMapName);
}
}
}
When I go to the immediate window for the property that has a custom attribute I can do
?Property.GetCustomAttributes(true)[0]
It will return ColumnMapName: "OtroID"
I can't seem to fit this to work programmatically though
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你想这样做:
You want to do this I believe:
应作者要求,从原始问题的评论中重新发布
只是出于兴趣,typeof(T) 中的 T 是什么?
在立即窗口中,您正在调用 Property.GetCustomAttribute(true)[0],但在 foreach 循环内,您正在对类型参数调用 GetCustomattributes。
这行:
应该是这样
最好的问候,
Repost from Comments of original question, at authors request
Just out of interest what is T in typeof(T)?
In the immediate window you are calling Property.GetCustomAttribute(true)[0] but inside the foreach loop you are calling GetCustomattributes on a typeparameter instead.
This line:
Ought to be this
Best regards,
我得到的这段代码最终的结果是
x
的值为“OtroID Maps to CityID”
。I got this code to end up with the value of
x
being"OtroID Maps to CityID"
.从内部来看,您应该研究属性,而不是 typeof(T)。
使用智能感知并查看可以从 Property 对象调用的方法。
Property.GetCustomAttributes(Boolean) 可能对您很重要。
这将返回一个数组,您可以在其上使用 LINQ 来快速返回符合您要求的所有属性。
In the inner look, you should be investigating the Properties, not the typeof(T).
Use the intellisense and take a look at the methods you can call off of the Property objects.
Property.GetCustomAttributes(Boolean) might be of importance to you.
This will return an array, and you can use LINQ on it to quickly return all the attributes that match your requirements.