问题
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
回答1:
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:
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(T));
Ought to be this
System.Attribute[] attrs = property.GetCustomAttributes(true);
Best regards,
回答2:
You want to do this I believe:
PropertyInfo[] propCollection = type.GetProperties();
foreach (PropertyInfo property in propCollection)
{
foreach (var attribute in property.GetCustomAttributes(true))
{
if (attribute is ColumnName)
{
}
}
}
回答3:
I got this code to end up with the value of x
being "OtroID Maps to CityID"
.
var props = typeof(City).GetProperties();
foreach (var prop in props)
{
var attributes = Attribute.GetCustomAttributes(prop);
foreach (var attribute in attributes)
{
if (attribute is ColumnName)
{
ColumnName a = (ColumnName)attribute;
var x = string.Format("{1} Maps to {0}",prop.Name,a.ColumnMapName);
}
}
}
回答4:
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.
来源:https://stackoverflow.com/questions/8948522/c-sharp-custom-attributes-from-properties