获取属性自身名称问题

ABC_678 2015-05-20 10:33:20

public static string Bill_codes { get; set; }

public static string GetProertyName(object Proerty)
{
return GetPropertyName<object>(() => Proerty);
}

private static string GetPropertyName<T>(Expression<Func<T>> express)
{
switch (express.Body.NodeType)
{
case ExpressionType.MemberAccess:
return ((MemberExpression)express.Body).Member.Name;
case ExpressionType.Convert:
return ((MemberExpression)((UnaryExpression)express.Body).Operand).Member.Name;
default:
return string.Empty;
}
}


private void test()
{
GetProertyName(Common.Bill_codes); /* 期望返回字符串: “Bill_codes” ,但实际返回了 “Proerty”*/
}
...全文
103 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ABC_678 2015-05-20
  • 打赏
  • 举报
回复
引用 1 楼 hefeng_aspnet 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
    class Program
    {
        class Test
        {
            public string PropertyJustForTest1 { get; set; }
            public Test PropertyJustForTest2 { get; set; }
        }
        static void Main(string[] args)
        {
            Test test = new Test();
            Console.WriteLine(GetPropertyNameHelper.GetPropertyName<object>(() => test.PropertyJustForTest1));
            Console.WriteLine(GetPropertyNameHelper.GetPropertyName<object>(() => test.PropertyJustForTest2));
        }
    }
    static class GetPropertyNameHelper
    {
        public static string GetPropertyName<T>(Expression<Func<T>> express)
        {
            var memberExpress = express.Body as MemberExpression;
            if (memberExpress != null)
            {
                return memberExpress.Member.Name;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
擦,找到原因了,不该封装这个方法,封装后对象类型传递变味了。 public static string GetProertyName(object Proerty) { return GetPropertyName<object>(() => Proerty); }
  • 打赏
  • 举报
回复
又见一个研究lamb树的人
於黾 2015-05-20
  • 打赏
  • 举报
回复
不明白你是要闹哪样 Bill_codes的名字不就是"Bill_codes"吗,还需要写一大堆函数去获取? 这样做有任何意义?
csdn_aspnet 2015-05-20
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
    class Program
    {
        class Test
        {
            public string PropertyJustForTest1 { get; set; }
            public Test PropertyJustForTest2 { get; set; }
        }
        static void Main(string[] args)
        {
            Test test = new Test();
            Console.WriteLine(GetPropertyNameHelper.GetPropertyName<object>(() => test.PropertyJustForTest1));
            Console.WriteLine(GetPropertyNameHelper.GetPropertyName<object>(() => test.PropertyJustForTest2));
        }
    }
    static class GetPropertyNameHelper
    {
        public static string GetPropertyName<T>(Expression<Func<T>> express)
        {
            var memberExpress = express.Body as MemberExpression;
            if (memberExpress != null)
            {
                return memberExpress.Member.Name;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}

111,111

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧