惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

博客园 - Ant

ASP.NET Core 项目简单实现身份验证及鉴权 使用iconfont图标 IIS调试ASP.NET Core项目 配置GitHub的SSH key Visual Studio连接Oracle数据库 Visual Studio Code 如何将新项目发布到GIT服务器 Xamarin踩坑经历 Abp项目中的领域模型实体类访问仓储的方法 在Abp中集成Swagger UI功能 发布以NLog作为日记工具的ASP.NET站点到IIS注意事项 Android Studio 简介及导入 jar 包和第三方开源库方[转] 解决jquery-ui-autocomplete选择列表被Bootstrap模态窗遮挡的问题 ORACLE存储过程创建失败,如何查看其原因 EF6配合MySQL或MSSQL(CodeFirst模式)配置指引 火狐通行证升级为Firefox Sync后,如何在多设备间同步书签等信息 (原创)通用查询实现方案(可用于DDD)[附源码] -- 设计思路 (原创)通用查询实现方案(可用于DDD)[附源码] -- 简介 Entity Framework 6.0 源码解读笔记(一) [转]Sql server2005中如何格式化时间日期
利用Lambda获取属性名称
Ant · 2014-09-01 · via 博客园 - Ant

感谢下面这篇博文给我的思路:

http://www.cnblogs.com/daimage/archive/2012/04/10/2440186.html

上面文章的博主给出的代码是可用的,但是调用方法时需要写的代码过于冗长,例如博主给出的示例代码

var name = TypeInfoHelper.GetClassPropertiesName<MyClass,List<string>>(s => myClass.UserName);

代码中的List<string>为MyClass.UserName属性的类型,如果要获取一个类型为int的Flag属性还得相应的改为下面这个样子

var name = TypeInfoHelper.GetClassPropertiesName<MyClass,int>(s => customer.Flag);

我想把其中的传入泛型参数的代码拿掉,变成这样子:

TypeInfoHelper.GetPropertyName(s=>myClass.UserName)

是不是变得非常简洁?下面即是全部代码,再次感谢代码哥,致敬!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var cust = new Customer();
            var addr = new Address(cust);
            string name1 = TypeInfoHelper.GetPropertyName(p => cust.Flag);
            Console.WriteLine(name1);
            string name2 = TypeInfoHelper.GetPropertyName(p => addr.Owner.Remark);
            Console.WriteLine(name2);
            string name3 = TypeInfoHelper.GetPropertyName(p => cust.Orders);
            Console.WriteLine(name3);
            string name4 = TypeInfoHelper.GetPropertyName(p => cust.Orders.FirstOrDefault().Price);
            Console.WriteLine(name4);
            Console.ReadLine();
        }
    }

    public class TypeInfoHelper
    {
        public static string GetPropertyName(Expression<Func<Object, Object>> propery)
        {
            var body = propery.Body.ToString();
            return body.Substring(body.LastIndexOf(".") + 1);
        }

    }

    public class Customer
    {
        public string Name { get; set; }
        public string Remark { get; set; }
        public int Flag { get; set; }

        public List<string> Contacts { get; set; }
        public List<Order> Orders { get; set; }
    }

    public class Order
    {
        public decimal Price { get; set; }
    }

    public class Address
    {
        private Customer customer;

        public string Country { get; set; }
        public string State { get; set; }
        public string Street { get; set; }

        public Customer Owner
        {
            get { return customer; }
        }

        public Address(Customer owner)
        {
            this.customer = owner;
        }
    }

}