8

所以我使用以下实用程序从类的实例中获取字段/属性的名称......

public static string FieldName<T>(Expression<Func<T>> Source)
{
    return ((MemberExpression)Source.Body).Member.Name;
}

这允许我执行以下操作:

public class CoolCat
{
    public string KaratePower;
}

public class Program
{
    public static Main()
    {
        public CoolCat Jimmy = new CoolCat();

        string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower);
    }
}

当我需要字段名称的字符串表示时,这对于序列化和其他时候非常有用。

但是现在,我希望能够在没有类实例的情况下获取字段名称 - 例如,如果我正在设置一个表并希望将列的 FieldNames 动态链接到类中的实际字段(所以重构等不会破坏它)。

基本上,我觉得我只是不太了解如何完成此操作的语法,但我想它看起来像这样:

public static string ClassFieldName<T>(Func<T> PropertyFunction)
{
     // Do something to get the field name?  I'm not sure whether 'Func' is the right thing here - but I would imagine that it is something where I could pass in a lambda type expression or something of the sort?
}

public class Program
{
    public static Main()
    {
        string CatsPowerFieldName = ClassFieldName<CoolCat>((x) => x.KaratePower);

        // This 'CatsPowerFieldName' would be set to "KaratePower".
    }
}

我希望这是有道理的——我对这个主题的词汇不是很好,所以我知道这个问题有点含糊。

4

3 回答 3

4

你想要做的是微软创建 System.Reflection 的原因之一试试这个

using System.Reflection;  // reflection namespace

public static List<Type> GetClassPropertyNames(Type myClass)
        {
            PropertyInfo[] propertyInfos;
            propertyInfos = myClass.GetProperties(BindingFlags.Public);

            List<Type> propertyTypeNames = new List<Type>();

            // write property names
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                propertyTypeNames .Add(propertyInfo.PropertyType);
            }

            return propertyNames;

        }
于 2012-06-05T14:22:54.197 回答
3

我有两种方法可以做到这一点。

第一个是可用于任何对象的扩展方法。

public static string GetPropertyName<TEntity, TProperty>(this TEntity entity, Expression<Func<TEntity, TProperty>> propertyExpression)
{
    return propertyExpression.PropertyName();
}

哪个像

public CoolCat Jimmy = new CoolCat();
string JimmysKaratePowerField = Jimmy.GetPropertyName(j => j.KaratePower);

我没有对象时使用的第二个。

public static string PropertyName<T>(this Expression<Func<T, object>> propertyExpression)
{
        MemberExpression mbody = propertyExpression.Body as MemberExpression;

        if (mbody == null)
        {
            //This will handle Nullable<T> properties.
            UnaryExpression ubody = propertyExpression.Body as UnaryExpression;

            if (ubody != null)
            {
                mbody = ubody.Operand as MemberExpression;
            }

            if (mbody == null)
            {
                throw new ArgumentException("Expression is not a MemberExpression", "propertyExpression");
            }
        }

        return mbody.Member.Name;
}

这可以像这样使用

string KaratePowerField = Extensions.PropertyName<CoolCat>(j => j.KaratePower);
于 2012-06-05T17:41:02.937 回答
2

我相信在这里使用反射会很有用。我现在没有 VS,但我相信您可以执行 typeof(class).GetMembers() 之类的操作。我的倒影有点生锈。

于 2012-06-05T14:23:26.783 回答