Ahjesus获取自定义属性Attribute或属性的名称
1:设置自己的自定义属性
public class NameAttribute:Attribute {
private string _description;
public NameAttribute(string description) {
_description = description;
}
public string Description {
get { return _description; }
}
}
2:设置获取属性或属性名的类
/// <summary>
/// 获取自定义属性Name或者属性名称
/// </summary>
/// <typeparam name="T">类型</typeparam>
public class AttributeHelper<T> where T : new() {
/// <summary>
/// 获取枚举类型自定义属性Name
/// </summary>
/// <param name="type">枚举</param>
/// <returns></returns>
public string NameFor(object type) {
T test = (T)type;
FieldInfo fieldInfo = test.GetType().GetField(test.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);
//IList<CustomAttributeData> list = fieldInfo.GetCustomAttributesData();
string des = (attribArray[] as NameAttribute).Description;
return des;
} /// <summary>
/// 获取属性自定义属性Name
/// </summary>
/// <param name="predicate">表达式</param>
/// <returns></returns>
public string NameFor(Expression<Func<T, object>> expr) {
string name = PropertyNameFor(expr);
T et = new T();
Type type = et.GetType();
PropertyInfo[] properties = type.GetProperties();
object[] attributes = null;
foreach (PropertyInfo p in properties) {
if (p.Name == name) {
attributes = p.GetCustomAttributes(typeof(NameAttribute), true);
break;
}
}//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
string des = ((NameAttribute)attributes[]).Description;
return des;
} /// <summary>
/// 获取属性的名称
/// </summary>
/// <param name="expr"></param>
/// <returns></returns>
public string PropertyNameFor(Expression<Func<T, object>> expr) {
var rtn = "";
if (expr.Body is UnaryExpression) {
rtn = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
}//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
else if (expr.Body is MemberExpression) {
rtn = ((MemberExpression)expr.Body).Member.Name;
}
else if (expr.Body is ParameterExpression) {
rtn = ((ParameterExpression)expr.Body).Type.Name;
}
return rtn;
}
}
3:设置测试实体类和枚举
public class MyEntity {
public MyEntity() {
Name = "Jude";
Age = ;
}
[Name("姓名")]
public string Name { get; set; }
[Name("年龄")]
public int Age { get; set; }
}
public enum MyEnum {
[Name("欧洲")]
Europe = ,
[Name("亚洲")]
Asia = ,
[Name("美洲")]
America =
}
4:开始测试
public partial class WebForm1 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
AttributeHelper<MyEntity> myEntityAttr = new AttributeHelper<MyEntity>();
MyEntity myEntity = new MyEntity();
AttributeHelper<MyEnum> myEnumAttr = new AttributeHelper<MyEnum>();
Response.Write(myEntityAttr.NameFor(it => it.Name) + ":" + myEntity.Name + "\n");//姓名:Jude
Response.Write(myEntityAttr.NameFor(it => it.Age) + ":" + myEntity.Age + "\n");//年龄:11
Response.Write(myEntityAttr.PropertyNameFor(it => it.Name) + ":" + myEntity.Name + "\n");//Name:Jude
Response.Write(myEntityAttr.PropertyNameFor(it => it.Age) + ":" + myEntity.Age + "\n");//Age:11
//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
Response.Write(myEnumAttr.NameFor(MyEnum.America) + ":" + MyEnum.America + "\n");//美洲:America
Response.Write(myEnumAttr.NameFor(MyEnum.Asia) + ":" + MyEnum.Asia + "\n");//亚洲:Asia
Response.Write(myEnumAttr.NameFor(MyEnum.Europe) + ":" + MyEnum.Europe + "\n");//欧洲:Europe
}
}
Ahjesus获取自定义属性Attribute或属性的名称的更多相关文章
- JS DOM属性,包括固有属性和自定义属性,以及属性获取、移除和设置
属性分为固有属性property和自定义属性attribute 固有属性查看 固有属性可以通过ele.property 来获取,自定义属性不行 <!DOCTYPE html> <ht ...
- 【转】C#通过Expression获取指定属性的名称
原文:http://www.cnblogs.com/powerwu/articles/3393582.html 大家所熟悉的是通过对象属性来访问该属性的值,或是由字符串通过反射来获取属性,并取值.今天 ...
- c# 获取某个对象的[公有属性]的名称,类型,值
/// <summary> /// 获取某个对象的[公有属性]的名称,类型,值 /// </summary> /// <typeparam name="T&qu ...
- 获取JSON对象的属性名称
1.问题背景 一个json对象,是以键值对组成,通过循环json对象,获取json对象中的属性名称 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 通过属性集名称获取属性集id
Mage::getModel('eav/entity_attribute_set')->load('属性集名称', 'attribute_set_name')->getAttributeS ...
- C# 自定义属性Attribute
自定义属性 /// <summary> /// 脱敏属性 /// </summary> public class SensitiveAttribute:Attribute { ...
- ZeroMQ接口函数之 :zmq_getsockopt – 获取ZMQ socket的属性
ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html 本文地址 :http://www.cnblogs.com/fengbo ...
- 福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型
我们知道,KVC+Runtime可以做非常多的事情.有了这个,我们可以实现很多的效果. 这里来个福利,利用KVC+Runtime获取类/对象的所有成员变量.属性.方法及协议: 并利用它来实现字典转模型 ...
- jquery】常用的jquery获取表单对象的属性与值
[jquery]常用的jquery获取表单对象的属性与值 1.JQuery的概念 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用一些简单的代码实现一些复杂的 ...
随机推荐
- win7 VS2012+openCV-2.4.11 配置
1.下载 http://opencv.org/downloads.html (根据版本的不同选择,这里选择的是opencv-2.4.11) 2.安装 3.环境变量配置 计算机->属性->高 ...
- xml和xsd架构文档相关知识
1.使用架构(XSD)验证XML文件 2.使用自动生成工具: 工具目录:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4. ...
- 学习使用:before和:after伪元素
如果你一直密切关注着各种网页设计的博客,你可能已经注意到了:before和:after伪元素已经在前端开发中获得了相当多的关注.特别是在Nicolas Gallagher的博客中,后期运用了很多伪类元 ...
- iOS 企业证书发布app 流程
企业发布app的 过程比app store 发布的简单多了,没那么多的要求,哈 但是整个工程的要求还是一样,比如各种像素的icon啊 命名规范啊等等. 下面是具体的流程 1.修改你的 bundle i ...
- toad 常用快捷键与配置
F8 调出以前执行的sql命令 F9 执行全部sql Ctrl+. 补全table_name Ctrl+t 补全table_name,或者显示字段 alt+ 箭头上下 看sql history Ctr ...
- 条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...
- Android & CM build basics
[CM source code folders] bootable/Among other things, the source for ClockworkMod recovery is in her ...
- 栈stack的C实现
头文件—————————————————————————— #ifndef _STACK_H_ #define _STACK_H_ #include <stdlib.h> #define ...
- 基于tiny4412的Linux内核移植 --- aliases节点解析
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- [转载]SharePoint 2013测试环境安装配置指南
软件版本 Windows Server 2012 标准版 SQL Server 2012 标准版 SharePoint Server 2013 企业版 Office Web Apps 2013 备注: ...