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的类库,这个类库集合了很多功能方法,利用类库你可以用一些简单的代码实现一些复杂的 ...
随机推荐
- 圆满完成Selenium自动化测试周末班培训课程!
圆满完成Selenium自动化测试周末班培训课程! http://automationqa.com/forum.php?mod=viewthread&tid=2704&fromuid= ...
- android-tools-adb
sudo apt-get install android-tools-adbsudo apt-get install android-tools-fastboot
- CI 框架去掉url 中index.php的方法
1 修改 apache 的 httpd.conf 文件 #LoadModule rewrite_module modules/mod_rewrite.so 去掉前面的# 2 找到 你程序目录下的 .h ...
- Java NIO原理分析
Java IO 在Client/Server模型中,Server往往需要同时处理大量来自Client的访问请求,因此Server端需采用支持高并发访问的架构.一种简单而又直接的解决方案是“one-th ...
- tengine + mysql + nginx + php
tengine + mysql + nginx + php 1.配置防火墙vim /etc/sysconfig/iptables # 允许80端口通过防火墙-A INPUT -m state --st ...
- php分页类代码带分页样式效果(转)
php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...
- 【原】Oracle11gR2图文安装
作者:david_zhang@sh [转载时请以超链接形式标明文章] http://www.cnblogs.com/david-zhang-index/p/4365475.html 本文以看图说话的方 ...
- Objective-C Polymorphism
#import <Foundation/Foundation.h> @interface Shape : NSObject { CGFloat area; } -(void)printAr ...
- mark:如何使用FileZilla连接虚拟机上的Fedora
1. 下载FileZilla 2. 在虚拟机上安装SSH,http://linuxconfig.org/how-to-install-start-and-connect-to-ssh-server-o ...
- Appium移动自动化测试(二)--安装Android开发环境
继续Appium环境的搭建. 第二节 安装Android开发环境 如果你的环境是MAC那么可以直接跳过这一节.就像我们在用Selenium进行web自动化测试的时候一样,我们需要一个浏览器来执行测试 ...