【问题标题】:Get property name from expression for array property从数组属性的表达式中获取属性名称
【发布时间】:2016-07-09 22:38:00
【问题描述】:

基本上我正在尝试做非常简单的事情,但我不知道该怎么做。 我正在尝试从表达式中获取属性名称,例如对于这个表达式,我想获得结果:

  • PropertyName(e => e.Easy) = “简单”
  • PropertyName(e => e.Not.So.Easy) = "不是那么简单"
  • PropertyName(e => e.ImLost[i]) 其中 i 是 for-loop 中的变量 = "ImLost[0]", "ImLost[1]", ...

前两个相对容易,但第三个我迷路了,但我认为我不能成为第一个想要实现相同目标的人。你有什么建议或想法吗? PropertyName 方法的签名遵循static string PropertyName<T, TProp>(Expression<Func<T, TProp>> expr) where T : class

【问题讨论】:

  • 您目前如何实施PropertyName?你能展示你的代码吗?

标签: c# .net asp.net-mvc tree expression


【解决方案1】:

以下将处理所有三种情况:

using System;
using System.Collections;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;

namespace PropertyNameFromExpression
{
    [TestClass]
    public class PropertyNameTests
    {
        [TestMethod]
        public void TestCanGetEasyExpression()
        {
            string propertyName = PropertyName<Source>(e => e.Easy);
            Assert.AreEqual("Easy", propertyName);
        }
        [TestMethod]
        public void TestCanGetNotSoEasyExpression()
        {
            string propertyName = PropertyName<Source>(e => e.Not.So.Easy);
            Assert.AreEqual("Not.So.Easy", propertyName);
        }
        [TestMethod]
        public void TestCanGetArrayExpressionWithConstantIndex()
        {
            string propertyName = PropertyName<Difficult>(e => e.ImLost[3]);
            Assert.AreEqual("ImLost["+3+"]", propertyName);
        }
        [TestMethod]
        public void TestCanGetArrayExpressionWithVariableIndex()
        {
            int i = 3;
            string propertyName = PropertyName<Difficult>(e => e.ImLost[i]);
            Assert.AreEqual("ImLost[" + i + "]", propertyName);
        }

        public string PropertyName<TSource>(Expression<Func<TSource, int>> expression)
        {
            return PropertyName(expression.Body);
        }

        private string PropertyName(Expression expression)
        {
            if (expression is BinaryExpression)
            {
                return PropertyName(expression as BinaryExpression);
            }
            if (expression is ConstantExpression)
            {
                return (expression as ConstantExpression).Value.ToString();
            }
            MemberExpression memberExpression = expression as MemberExpression;
            if (memberExpression.Expression is MemberExpression)
            {
                return PropertyName(memberExpression.Expression) + "." + memberExpression.Member.Name;
            }
            if (memberExpression == null)
            {
                throw new ArgumentException("Unknown Expression type:"+expression.GetType().Name);
            }
            if (memberExpression.Member.MemberType == MemberTypes.Field)
            {
                var f = Expression.Lambda(memberExpression).Compile();
                object value = f.DynamicInvoke();
                return value.ToString();
            }
            return memberExpression.Member.Name;
        }
        private string PropertyName(BinaryExpression binaryExpression)
        {
            if (typeof(IEnumerable).IsAssignableFrom(binaryExpression.Left.Type))
            {
                return PropertyName(binaryExpression.Left) + "[" + PropertyName(binaryExpression.Right) + "]";
            }
            return PropertyName(binaryExpression.Left) + "." + PropertyName(binaryExpression.Right);
        }

    }

    public class Source
    {
        public int Easy { get; set; }
        public Level2 Not { get; set; }
    }

    public class Level2
    {
        public Level3 So { get; set; }
    }
    public class Level3
    {
        public int Easy { get; set; }
    }

    public class Difficult
    {
        public int[] ImLost { get;set; }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2012-01-08
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多