在C#3.5中从类中查找类的特定属性的名称

在C#3.5中从类中查找类的特定属性的名称,c#,.net,reflection,properties,propertyinfo,C#,.net,Reflection,Properties,Propertyinfo,给一个像下面这样的类,我怎样才能找到一个particluar属性的名称 public class Student { public int Grade { get; set; } public string TheNameOfTheGradeProperty { get { return ???? } } // More proper

给一个像下面这样的类,我怎样才能找到一个particluar属性的名称

public class Student
{
    public int Grade
    {
        get;
        set;
    }

    public string TheNameOfTheGradeProperty
    {
        get
        {
            return ????
        }
    }

    // More properties..    
}
因此,我想从TheNameOfGradeProperty属性返回字符串“Grade”。我问的原因是我不想使用硬编码字符串,而是使用lambda表达式或其他东西

我怎样才能做到这一点

using System.Reflection;

return typeof(Student).GetProperty("Grade").Name;
但正如您所看到的,使用反射(以这种方式)并不是遥遥领先,因为“Grade”字符串仍然是硬编码的,这意味着在这种情况下,仅返回“Grade”更有效


我喜欢做的一件事是创建一个自定义属性并将其添加到一个成员,如下所示以下内容可防止您使用硬编码字符串“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..    

} 

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..    

} 
但正如您所看到的,使用反射(以这种方式)并不是遥遥领先,因为“Grade”字符串仍然是硬编码的,这意味着在这种情况下,仅返回“Grade”更有效


我喜欢做的一件事是创建一个自定义属性并将其添加到一个成员,如下所示以下内容可防止您使用硬编码字符串“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..    

} 

您有一个非常奇怪的请求。您的意思是不想使用硬编码字符串,因为重构类时希望
TheNameofGradeProperty
保持最新?如果是这样,这里有一个奇怪的方法:

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..    

} 
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;
        }
    }
}
公共班级学生
{
公共整数等级{get;set;}
公共字符串TheNameOfGradeProperty
{
得到
{
表达式gradexpr=()=>this.Grade;
MemberExpression body=gradeExpr.body作为MemberExpression;
返回body.Member.Name;
}
}
}

您有一个非常奇怪的请求。您的意思是不想使用硬编码字符串,因为重构类时希望
TheNameofGradeProperty
保持最新?如果是这样,这里有一个奇怪的方法:

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;
        }
    }
}
公共班级学生
{
公共整数等级{get;set;}
公共字符串TheNameOfGradeProperty
{
得到
{
表达式gradexpr=()=>this.Grade;
MemberExpression body=gradeExpr.body作为MemberExpression;
返回body.Member.Name;
}
}
}

可以使用表达式查找属性名称,使用简单的扩展方法,您可以在任何对象上使用它。。。如果需要将其限制为一组对象,可以对T应用通用约束。希望这有帮助

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");
    }
}
公共班级学生
{
公共整数等级{get;set;}
公共字符串名称{get;set;}
公共字符串GradePropertyName
{
获取{返回this.PropertyName(s=>s.Grade);}
}
公共字符串名称PropertyName
{
获取{返回this.PropertyName(s=>s.Name);}
}
}
公共静态类助手
{
公共静态字符串PropertyName(此T实例,表达式)
{
var property=expression.Body作为MemberExpression;
if(属性!=null)
{
var info=property.Member作为PropertyInfo;
如果(信息!=null)
{
返回信息名称;
}
}
抛出新ArgumentException(“表达式不是属性”);
}
}

可以使用表达式查找属性名称,使用简单的扩展方法,您可以在任何对象上使用它。。。如果需要将其限制为一组对象,可以对T应用通用约束。希望这有帮助

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");
    }
}
公共班级学生
{
公共整数等级{get;set;}
公共字符串名称{get;set;}
公共字符串GradePropertyName
{
获取{返回this.PropertyName(s=>s.Grade);}
}
公共字符串名称PropertyName
{
获取{返回this.PropertyName(s=>s.Name);}
}
}
公共静态类助手
{
公共静态字符串PropertyName(此T实例,表达式)
{
var property=expression.Body作为MemberExpression;
if(属性!=null)
{
var info=property.Member作为PropertyInfo;
如果(信息!=null)
{
返回信息名称;
}
}
抛出新ArgumentException(“表达式不是属性”);
}
}

除非您以某种方式引用了要返回其名称的属性,否则这是不可能的,如果您这样做,还可以使用硬编码字符串。你可以使用反射,但目前它对我来说毫无意义。您的要求是什么?@johnc有时在数据绑定时需要属性的名称,并非所有重构工具在重命名时都考虑字符串文本properties@johnc…下面的答案显示了如何在不使用硬编码字符串的情况下完成请求。除非您以某种方式引用了要返回其名称的属性,否则这是不可能的,如果您这样做,您也可以只使用硬编码字符串。你可以使用反射,但目前它对我来说毫无意义。您的要求是什么?@johnc有时在数据绑定时需要属性的名称,并非所有重构工具在重命名时都考虑字符串文本properties@johnc…除了下面的答案,这些答案显示了如何在不使用硬编码字符串的情况下完成请求。对于这个答案,我忍不住笑了。@johnc:这是老式的,考虑到3.x的功能,很可能是一个属性的错误用法,但它仍然是一个选项。这不是一个“最值得骄傲的时刻”:代码没有问题,我只是对简单的返回“等级”的奇妙混淆感到好笑;。我肯定他有一个要求,我只是不知道现在是什么,面对这个答案我忍不住笑了。@johnc:这是老式的,考虑到3.x的功能,它显得有些苍白,很可能是