您现在的位置:首页 --> JavaScript --> 获取匿名对象的属性
获取匿名对象的属性
浏览:1815次 出处信息
以下是代码片段: 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属性 (阅读:6549)
- 细说JavaScript中对象的属性和方法 (阅读:2281)
- [Perl6]类, 属性, 方法和其它 (阅读:1685)
- 细说魅力属性 (阅读:1512)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:Inline Form Labels
后一篇:JS中如何判断字符串类型的数字 >>
文章信息
- 作者:小寒 来源: 记事本
- 标签: 匿名对象 属性
- 发布时间:2010-08-04 23:17:54
近3天十大热文
-
[77] memory prefetch浅析
-
[51] find命令的一点注意事项
-
[37] 基本排序算法的PHP实现
-
[33] 卡诺模型―设计品质与设计价值的思考
-
[32] Oracle bbed工具的编译
-
[26] 8大实用又重要Mac使用技巧
-
[26] 程序员技术练级攻略
-
[25] 两行 JavaScript 代码
-
[24] 读书笔记-壹百度:百度十年千倍的29条法则
-
[21] 小屏幕移动设备网页设计注意事项