繁体   English   中英

从没有实例的类中获取字段的名称

[英]Get the name of a field from a class without an instance

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

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

我希望这是有道理的 - 我对这个主题的词汇不是很了解所以我知道这个问题有点模糊。

你要做的是微软创建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;

        }

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

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

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

我相信使用Reflection在这里很有用。 我现在没有和我在一起,但我相信你可以做类似的事情(类).GetMembers()。 我的反思有点生疏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM