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的类库,这个类库集合了很多功能方法,利用类库你可以用一些简单的代码实现一些复杂的 ...
随机推荐
- javascript方法 call()和apply()的用法
先上代码: apply()方法示例 /*定义一个人类*/ function Person(name,age) { this.name=name; this.age=age; } /*定义一个学生类*/ ...
- ThreadPoolExecutor
ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...
- archlinux 下 nignx + php 出现 no input file specified
奇葩的问题,配置 nginx + php + mysql 后,加一个站点: server { listen 80; server_name wei.abc.com; root /www/wei.abc ...
- 使用SharePoint Designer定制开发专家库系统实例!
将近大半年都没有更新博客了,趁这段时间不忙,后续会继续分享一些技术和实际应用.对于Sharepoint的定制开发有很多种方式,对于一般的应用系统,可以使用Sharepoint本身自带的功能,如列表作为 ...
- 2012年中国科学技术信息研究所发布自然科学SCI(E)论文期刊分区列表
分区 期刊名称 ISSN 学科名称 学科名称(中文) 总被引频次 影响因子 5年影响因子 即年影响因子 被引半衰期 特征因子 论文影响指标 1区 ACM COMPUT SURV 0360-0300 C ...
- java框架篇---spring IOC 实现原理
IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...
- 安卓模拟器bluestack 换imei
有好多种方法,下面介绍2种 第一种方法 通过靠谱助手设置,非常简单. 第二种方法 1.解压 root_20121221文件夹,将Root.fs 覆盖到 win7路径:C:\Progra ...
- Android应用安全之外部动态加载DEX文件风险
1. 外部动态加载DEX文件风险描述 Android 系统提供了一种类加载器DexClassLoader,其可以在运行时动态加载并解释执行包含在JAR或APK文件内的DEX文件.外部动态加载DEX文件 ...
- Lua中的weak表——weak table
弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...
- Visual Studio 2010 简体中文旗舰、专业版(MSDN原版下载)
Visual Studio 2010 简体中文旗舰.专业版(MSDN原版下载)(Visual Studio 2010 ultimate professional x86 dvd)2010[光盘镜像]- ...