您现在的位置:首页 --> JavaScript --> 获取匿名对象的属性
获取匿名对象的属性
浏览:1827次 出处信息
以下是代码片段: 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中对象的属性和方法 (阅读:2282)
- [Perl6]类, 属性, 方法和其它 (阅读:1687)
- 细说魅力属性 (阅读:1518)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:Inline Form Labels
后一篇:JS中如何判断字符串类型的数字 >>
文章信息
- 作者:小寒 来源: 记事本
- 标签: 匿名对象 属性
- 发布时间:2010-08-04 23:17:54
近3天十大热文
-
[67] memory prefetch浅析
-
[53] 转载:cassandra读写性能原理分析
-
[50] 深入浅出cassandra 4 数据一致性问
-
[43] 字符引用和空白字符
-
[42] MySQL半同步存在的问题
-
[41] 基本排序算法的PHP实现
-
[40] JS中如何判断字符串类型的数字
-
[40] 获取Dom元素的X/Y坐标
-
[38] javascript插入样式
-
[35] Inline Form Labels