使用表达式树,获取类的属性说明
来源:互联网 发布:软件源代码怎么找 编辑:程序博客网 时间:2024/06/16 15:08
首先定义下面的方法:
string GetDescription<T,F>(Expression<Func<T,F>> property){ var att = GetDescriptionAttrbute(property); return att.Description;} private 【DescriptionAttrbute】 GetDescriptionAttrbute(Expression<Func<T, object>> property) { if (property== null) return null; var body = property.Body as MemberExpression; if (body == null) { var temp = property.Body as UnaryExpression; if (temp != null) { body = temp.Operand as MemberExpression; } } if (body == null) { throw new System.Exception(string.Format("Error Expression:{0}!", property)); } return body.Member.GetCustomAttributes(typeof(DescriptionAttrbute),true) as DescriptionAttrbute; }
然后这样使用:
public class Test{ [Description("测试")] public string Key="123";}public class AttributeHelperTest{ public static void Test() { Test test=new global::Test.Test(); string content=AtttributeHelper.GetDestcripiton<Test>(info=>info.Key); Console.Write(content=="测试 "); }}
- 使用表达式树,获取类的属性说明
- 使用属性表达式获取属性名 C# .net
- EL表达式获取对象属性的原理
- EL表达式获取对象属性的原理
- 在jsp页面使用el表达式获取javaBean属性值
- 获取类的属性
- @property使用属性说明
- 属性使用范围说明
- 获取指定标签的指定属性的正则表达式
- Java 使用反射获取类、方法、属性上的注释
- el表达式获取的属性有特殊符号如引号
- 匹配获取HTML标签属性的正则 表达式
- EL表达式获取属性时所做的事情
- JAVA反射机制-EL表达式获取对象属性的原理
- 使用正则表达式获取链接的Host
- 正则表达式的说明
- Java使用PropertyDescriptor获取实体类中私有属性的值,并给私有属性赋值
- EditText的属性说明
- 移动开发之手机游戏基本概念
- UltraEdit使用技巧
- xp下修复win7引导
- 为你的职涯做个清楚的定义
- 前端网址收藏cool80的
- 使用表达式树,获取类的属性说明
- linux下aMule的设置
- 常用的4种动态网页技术—CGI、ASP、JSP、PHP
- C# some details
- 9.OD-断点、命令
- 在windows下,用Bochs安装linux
- Tomcat服务器的安装及配置
- tomcat 和 glassfish的简单使用历史
- 快速排序非递归实现