Reputation: 15706
Is there any attributes for custom name of a class property?
I am now using reflection to get the property name.
PropertyInfo[] propInfos = typeof(T).GetProperties();
for (int i = 0; i <= propInfos.Length - 1; i++)
{
sb.Append(propInfos[i].Name);
.....
}
But I want to have custom name of that. I don't want "Lastname" but "Last Name" in my result. I am not familiar with the asp.net attribute.
Upvotes: 0
Views: 79
Reputation: 8129
Create an Attribute Class:
[AttributeUsage(AttributeTargets.Property)]
public class PropertyNameAttribute : Attribute
{
public PropertyNameAttribute(string name) { ... }
...
}
Decorate your Properties with that Attribute
[PropertyName("Last Name")]
public string LastName {get;set;}
With Reflection you can get those Properties this way:
var attr = (PropertyNameAttribute)propInfo.GetCustomAttributes(typeof(PropertyNameAttribute), false).Single();
Upvotes: 3