要转换为string的类属性的名称

时间:2014-06-25 14:25:24

标签: c# string properties

我无休止地搜索了一个完全指出我的问题的线程,但我找不到它。

我想获取一个类属性,然后将该属性作为字符串取出来!

public class Foo
{
   public Foo ()
   {
   }
   public int MyProperty { get; set; }
}

然后我想要字符串" MyProperty"最后,像这样:

Foo f = new Foo();
string s = Helperfunction(f.MyProperty);

string Helperfunction(  X  )
{
   string MyString;
   //do something with X
   return MyString;
}

通过可能的辅助功能实现这有多困难? 所以输出应该是" MyProperty"

因为我不想对它进行硬编码,并且只要我重构属性名称,它就可以在不编辑硬编码字符串的情况下工作!

2 个答案:

答案 0 :(得分:5)

您可以使用反射来获取名称,这是我使用的辅助类:

public static class MemberName
{
    /// <summary>
    /// *** WARNING - Uses reflection - use responsibly ***
    /// </summary>
    /// <param name="instance"></param>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static string GetMemberName<T>(this T instance, Expression<Func<T, object>> expression)
    {
        return GetMemberName(expression);
    }

    /// <summary>
    /// *** WARNING - Uses reflection - use responsibly ***
    /// </summary>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    /// <exception cref="ArgumentException"></exception>
    public static string GetMemberName<T>(Expression<Func<T, object>> expression)
    {
        if (expression == null)
        {
            throw new ArgumentException("The expression cannot be null.");
        }

        return GetMemberName(expression.Body);
    }

    /// <summary>
    /// *** WARNING - Uses reflection - use responsibly ***
    /// </summary>
    /// <param name="instance"></param>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static string GetMemberName<T>(this T instance, Expression<Action<T>> expression)
    {
        return GetMemberName(expression);
    }

    /// <summary>
    /// *** WARNING - Uses reflection - use responsibly ***
    /// </summary>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    /// <exception cref="ArgumentException"></exception>
    public static string GetMemberName<T>(Expression<Action<T>> expression)
    {
        if (expression == null)
        {
            throw new ArgumentException("The expression cannot be null.");
        }

        return GetMemberName(expression.Body);
    }

    private static string GetMemberName(Expression expression)
    {
        if (expression == null)
        {
            throw new ArgumentException("The expression cannot be null.");
        }

        if (expression is MemberExpression)
        {
            // Reference type property or field
            var memberExpression = (MemberExpression)expression;
            return memberExpression.Member.Name;
        }

        if (expression is MethodCallExpression)
        {
            // Reference type method
            var methodCallExpression = (MethodCallExpression)expression;
            return methodCallExpression.Method.Name;
        }

        if (expression is UnaryExpression)
        {
            // Property, field of method returning value type
            var unaryExpression = (UnaryExpression)expression;
            return GetMemberName(unaryExpression);
        }

        throw new ArgumentException("Invalid expression");
    }

    private static string GetMemberName(UnaryExpression unaryExpression)
    {
        if (unaryExpression.Operand is MethodCallExpression)
        {
            var methodExpression = (MethodCallExpression)unaryExpression.Operand;
            return methodExpression.Method.Name;
        }

        return ((MemberExpression)unaryExpression.Operand).Member.Name;
    }
}

一个示例用法:

 string propName = MemberName.GetMemberName<Foo>(x => x.MyProperty);

答案 1 :(得分:4)

我不太明白这个问题,但是如果你想对属性名做些什么,你可以查看CallerMemberName属性。

string Helperfunction([CallerMemberName]string X = null  )
{

   return "PropertyName: " + X;

}

如果在属性getter中调用此方法,则它将输出属性的名称:

public class Foo
{
     public string Foo { get { Trace.WriteLine (SomeFunction()); } }
}

将输出“MyProperty”