博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ahjesus获取自定义属性Attribute或属性的名称
阅读量:6258 次
发布时间:2019-06-22

本文共 3854 字,大约阅读时间需要 12 分钟。

1:设置自己的自定义属性

public class NameAttribute:Attribute {        private string _description;        public NameAttribute(string description) {            _description = description;        }                public string Description {            get { return _description; }        }    }

2:设置获取属性或属性名的类

///     /// 获取自定义属性Name或者属性名称    ///     /// 
类型
public class AttributeHelper
where T : new() { ///
/// 获取枚举类型自定义属性Name /// ///
枚举 ///
public string NameFor(object type) { T test = (T)type; FieldInfo fieldInfo = test.GetType().GetField(test.ToString()); object[] attribArray = fieldInfo.GetCustomAttributes(false); //IList
list = fieldInfo.GetCustomAttributesData(); string des = (attribArray[0] as NameAttribute).Description; return des; } ///
/// 获取属性自定义属性Name /// ///
表达式 ///
public string NameFor(Expression
> 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; } }//出自 尊重作者辛苦劳动成果,转载请注明出处,谢谢! string des = ((NameAttribute)attributes[0]).Description; return des; } ///
/// 获取属性的名称 /// ///
///
public string PropertyNameFor(Expression
> expr) { var rtn = ""; if (expr.Body is UnaryExpression) { rtn = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name; }//出自 尊重作者辛苦劳动成果,转载请注明出处,谢谢!        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 = 11;        }        [Name("姓名")]        public string Name { get; set; }        [Name("年龄")]        public int Age { get; set; }    }
public enum MyEnum {        [Name("欧洲")]        Europe = 0,        [Name("亚洲")]        Asia = 1,        [Name("美洲")]        America = 2    }

4:开始测试

public partial class WebForm1 : System.Web.UI.Page {        protected void Page_Load(object sender, EventArgs e) {            AttributeHelper
myEntityAttr = new AttributeHelper
(); MyEntity myEntity = new MyEntity(); AttributeHelper
myEnumAttr = new AttributeHelper
(); 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 } }

 

 

 

 

转载于:https://www.cnblogs.com/ahjesus/p/3363015.html

你可能感兴趣的文章
ASP.NET之Javascript脚本的应用
查看>>
vlan间的互通
查看>>
ldconfig详解
查看>>
VBScript 页面的简单样例
查看>>
用c语言指针实现给整形数组冒泡排序
查看>>
ORA-01075,ORA-09925 Read-only file system问题一例
查看>>
Script:收集介质恢复诊断信息
查看>>
SocketIO 随笔
查看>>
Maven学习三之新建maven项目
查看>>
HTML5本地存储-localStorage如何实现定时存储
查看>>
LAMP之Centos6.5安装配置Apache(二)
查看>>
Tomcat集群
查看>>
shell脚本中输出带颜色字体实例分享及chrony时间同步
查看>>
简单计时
查看>>
面试心得
查看>>
linux系统时间同步,硬件时钟和系统时间同步,时区的设置
查看>>
CentOS下载包格式说明
查看>>
VMware Vsphere 6.0安装配置 二安装vcenter server程序
查看>>
关于CISCO asa5510防火墙端口映射配置
查看>>
2012年6月美国最佳虚拟主机提供商TOP12性能评测
查看>>