您现在的位置:首页 --> JavaScript --> 获取匿名对象的属性
获取匿名对象的属性
浏览:1718次 出处信息
以下是代码片段: namespace System.Extension.Dynamic { public static class Dynamic { private static Dictionary<PropertyInfo, object> propertyGetters = new Dictionary<PropertyInfo, object>(); public static object Property(this object instance, string name) { var instanceType = instance.GetType(); var propertyInfo = instanceType.GetProperty(name); if (propertyInfo == null) throw new InvalidOperationException(); if (!propertyInfo.CanRead) throw new InvalidOperationException(); object compiled; if (!propertyGetters.TryGetValue(propertyInfo, out compiled)) { var parameter = Expression.Parameter(typeof(object), "obj"); var convertParameter = Expression.Convert(parameter, instance.GetType()); var property = Expression.Property(convertParameter, propertyInfo); var convertReturnValue = Expression.Convert(property, typeof(object)); var lambda = Expression.Lambda(convertReturnValue, parameter); compiled = lambda.Compile(); propertyGetters.Add(propertyInfo, compiled); } return ((Func<object, object>)compiled)(instance); } } } |
建议继续学习:
- 前端必须熟悉的10个CSS3属性 (阅读:2848)
- 细说JavaScript中对象的属性和方法 (阅读:1812)
- [Perl6]类, 属性, 方法和其它 (阅读:1422)
- 细说魅力属性 (阅读:1251)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:Inline Form Labels
后一篇:JS中如何判断字符串类型的数字 >>
文章信息
- 作者:小寒 来源: 记事本
- 标签: 匿名对象 属性
- 发布时间:2010-08-04 23:17:54
近3天十大热文
- [12] 产品设计之QQ邮箱登录页与淘宝登录页
- [12] Python连接 MySQL 数据库的超时问
- [10] 一个 VLA (可变长度数组)的实现
- [10] 手机客户端交互适配设计之我见
- [9] 一句话crontab实现防ssh暴力破解
- [9] 没有比解决瓶颈更高效的事情了
- [9] 谈谈Facebook的聊天系统架构
- [8] 电子商务关键数字优化(线上部分,下)
- [8] isset和is_null的不同
- [8] 四年前的今天,我开始找工作