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的类库,这个类库集合了很多功能方法,利用类库你可以用一些简单的代码实现一些复杂的 ...
随机推荐
- 使用Xcode6.1.1打包出现Your account already has a valid iOS Distribution certificate问题
1.问题描述: 使用客户证书在Xcode6.1.1上进行打包测试,出现如下问题,查看网上也很多类似错误且解决办法各异. 2.我的解决办法: 让客户将开发.发布证书重新revoke掉之后重新创新并给到p ...
- Cubieboard2裸机开发之(五)看门狗操作
前言 说到看门狗,应该不会陌生,看门狗说白了就是一个定时器,但是它有一个非常重要的功能就是复位系统.在A20里,看门狗的操作非常简单,只有两个寄存器,不需要操作时钟相关的东西,系统起来后可以直接使用, ...
- [初读笔记] Cloud Migration Research: A Systematic Review (TCC, 2013)
Pooyan Jamshidi, Aakash Ahmad, Claus Pahl, "Cloud Migration Research: A Systematic Review," ...
- windows 程序设计自学:窗口正中显示Hello,World
#include <windows.h> LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window UINT uMsg, // ...
- IPv4分析
IPv4的头部格式: 1. Version 版本号,默认是4. 2. IHL(Internet Header Length) 就是IPv4头部长度.这个长度的单位是32bit,一般是5,那么头部的长度 ...
- linux 下面 jdk1.7 rpm 包的安装
1.下载安装jdk7.0 for linux 我下载的版本为:jdk-7u2-linux-i586.rpm 下载地址为:http://www.oracle.com/technetwork/java/j ...
- react-native Unrecognized font family ‘Lonicons’;
在使用:react-native-vector-icons库时报的错. 解决办法:使用这个库时android和ios工程目录中需要做些配置 配制方法如下: http://www.cnblogs.com ...
- 解决方法:64位系统-未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
win7或win8 64位下调试程序,出现这样的错误提示:未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序 解决方法如下: 方法一:“设置应用程序池默认属性”/“常规”/ ...
- Delphi 10.1 Berlin Starter Edition
Delphi 10.1 Berlin Starter Edition Embarcadero® Delphi 10.1 Berlin Starter is a great way to get sta ...
- 30个实用的 Photoshop 动作《免费下载》
这篇文章向设计师们分享30个实用的 Photoshop 动作下载.Photoshop 的动作是用集合了许多步的操作的集合,使执行任务自动化,这为设计者在进行图像处理的操作上带来很多方便.同时用户还可以 ...