Give a class like below, how can i find the name of one particluar property?
public class Student
{
public int Grade
{
get;
set;
}
public string TheNameOfTheGradeProperty
{
get
开发者_如何学编程 {
return ????
}
}
// More properties..
}
So i would like to return the string "Grade" from the TheNameOfTheGradeProperty property. The reason i'm asking is that i do not want to use a hardcoded string, but rather a lambda expression or something else.
How can i acheive this?
It is possible to use an expression to find the name of the property, using a simple extension method you can use it on any object... if you need to restrict it to a set of objects you can apply a generic constraint on T. Hope this helps
public class Student
{
public int Grade { get; set;}
public string Name { get; set; }
public string GradePropertyName
{
get { return this.PropertyName(s => s.Grade); }
}
public string NamePropertyName
{
get { return this.PropertyName(s => s.Name); }
}
}
public static class Helper
{
public static string PropertyName<T, TProperty>(this T instance, Expression<Func<T, TProperty>> expression)
{
var property = expression.Body as MemberExpression;
if (property != null)
{
var info = property.Member as PropertyInfo;
if (info != null)
{
return info.Name;
}
}
throw new ArgumentException("Expression is not a property");
}
}
You have a very strange request. Are you saying you want to not use a hard-coded string because you want TheNameOfTheGradeProperty
to stay up to date if you refactor the class? If so, here's a strange way to do so:
public class Student
{
public int Grade { get; set; }
public string TheNameOfTheGradeProperty
{
get
{
Expression<Func<int>> gradeExpr = () => this.Grade;
MemberExpression body = gradeExpr.Body as MemberExpression;
return body.Member.Name;
}
}
}
using System.Reflection;
return typeof(Student).GetProperty("Grade").Name;
But as you can see, you're not that far ahead using reflection (in this manner) because the "Grade" string is still hard-coded which means in this scenario it's more efficient just to return "Grade"
.
One thing I like to do is create and add a custom attribute to a member like so. The following prevents you from having to use the hard-coded string "Grade".
public class Student {
// TAG MEMBER WITH CUSTOM ATTRIBUTE
[GradeAttribute()]
public int Grade
{
get;
set;
}
public string TheNameOfTheGradeProperty
{
get
{
/* Use Reflection.
Loop over properties of this class and return the
name of the one that is tagged with the
custom attribute of type GradeAttribute.
*/
}
}
// More properties..
}
Creating custom attributes can be found here.
精彩评论