从没有实例的类中获取字段名称 [英] Get the name of a field from a class without an instance

查看:96
本文介绍了从没有实例的类中获取字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I use the following utility to get the name of a field/property from an instance of a class...

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

这允许我执行以下操作:

This allows me to do the following:

public class CoolCat
{
    public string KaratePower;
}

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

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

这对于序列化以及其他在我需要字段名称的字符串表示形式时非常有用.

This is great for serialization and other times when I need a string representation of the field name.

但是现在,我希望能够在没有类实例的情况下获得字段名称-例如,如果我要建立一个表并希望将列的FieldNames动态链接到类中的实际字段(因此重构等不会破坏它.)

But now, I want to be able to get the field name WITHOUT having an instance of the class - for instance, if I am setting up a table and want to dynamically link the FieldNames of the columns to actual fields in a class (so refactorings, etc. will not break it).

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

Basically, I feel like I just don't quite get the syntax of how to accomplish this, but I imagine that it will look something like this:

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

我希望这是有道理的-我对这个主题的词汇不太满意,所以我知道这个问题有点含糊.

I hope that makes sense - I'm not very good with the vocab around this subject so I know that the question is a little vague.

推荐答案

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

I have two methods I use to do this.

第一个是可以在任何对象上使用的扩展方法.

The first is an extension method that can be used on any object.

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);

没有对象时使用的第二个.

The second I use when I don't have an object.

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);

这篇关于从没有实例的类中获取字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆