我是靠谱客的博主 英俊帆布鞋,这篇文章主要介绍反射 属性描述 Description的使用方式,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
[Description("委托单位")] /// <summary> /// 委托单位 /// </summary> [Description("委托单位")] public string EntrustedUnit{get; set;}
复制代码
1
2
3
由于 描述可以有多个值,所以获取前需要判断是否含有。
复制代码
1
2
3
4
5
6
7
if ((DescriptionAttribute)Attribute.GetCustomAttribute(onePro, typeof(DescriptionAttribute)) != null) //当描述属性没有时,直接返回名称 { DESStr = ((DescriptionAttribute)Attribute.GetCustomAttribute(onePro, typeof(DescriptionAttribute))).Description; }

另一种 获取的方式:

复制代码
1
2
3
var v = (DescriptionAttribute[])onePro.GetCustomAttributes(typeof(DescriptionAttribute), false); var descriptionName = v[0].Description;

获取 所在 属性的值为:

复制代码
1
2
OnePr.GetValue(oneModel, null)

最后

以上就是英俊帆布鞋最近收集整理的关于反射 属性描述 Description的使用方式的全部内容,更多相关反射内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部