架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 31263|回复: 2

[资料] c# 反射获取对象属性的displayname

[复制链接]
发表于 2016-10-9 15:48:12 | 显示全部楼层 |阅读模式
QQ截图20161009153655.jpg

  1. //你感受一下
  2.     class Program
  3.     {
  4.         static void Main(String[] args)
  5.         {
  6.             var t = typeof(A);

  7.             var pName = t.GetProperty("Name");

  8.             //4.0或以上版本
  9.             var displayName = pName.GetCustomAttribute<DisplayNameAttribute>();

  10.             Console.WriteLine(displayName.DisplayName);

  11.             Console.ReadKey();
  12.         }

  13.     }


  14.     public class A
  15.     {
  16.         [DisplayName("张三")]
  17.         public String Name { get; set; }
  18.     }
复制代码


受上面的代码启发,为什么,我要获取displayname呢?

我重写了一个tostring方法。

原来这样写的:

  1. /// <summary>
  2.         /// 重写tostring
  3.         /// </summary>
  4.         /// <returns></returns>
  5.         public override string ToString()
  6.         {
  7.             return string.Format("id:{0},标题:{1},链接:{2},详情:{3},作者:{4},num:{5}", _id.ToString(), title, url, descrip{过滤}tion, by, num);
  8.         }
复制代码


这样是写死的,太麻烦了,如果,我类那么多,我每个都这样写,那不气死了。

最后,这样写,哈哈。

  1. [Serializable]
  2.     public class Test : Mongo.EntityBase
  3.     {
  4.         [DisplayName("标题")]
  5.         public string title { get; set; }

  6.         [DisplayName("链接")]
  7.         public string url { get; set; }

  8.         public string descrip{过滤}tion { get; set; }

  9.         public string by { get; set; }

  10.         public object num { get; set; }

  11.         /// <summary>
  12.         /// 重写tostring
  13.         /// </summary>
  14.         /// <returns></returns>
  15.         public override string ToString()
  16.         {
  17.             return GetDisplayName<Test>(this);
  18.         }

  19.         /// <summary>
  20.         /// 反射获取DisplayName
  21.         /// </summary>
  22.         /// <typeparam name="T"></typeparam>
  23.         /// <param name="t"></param>
  24.         /// <returns></returns>
  25.         private static string GetDisplayName<T>(T t) where T : class
  26.         {
  27.             List<String> list = new List<string>();
  28.             Type type = typeof(T);
  29.             PropertyInfo[] ps = type.GetProperties();
  30.             foreach (PropertyInfo info in ps)
  31.             {
  32.                 var name = info.GetCustomAttribute<DisplayNameAttribute>();
  33.                 list.Add((name == null ? info.Name : name.DisplayName) + ":" + (info.GetValue(t, null) == null ? string.Empty : info.GetValue(t, null).ToString()));
  34.             }
  35.             return string.Join(",", list);
  36.         }
  37.     }
复制代码






上一篇:.net/c# 实现mongodb查询和模糊查询4种方式[附源码]
下一篇:MongoDB权限角色表
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2016-10-9 15:54:01 | 显示全部楼层
                                
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2018-3-8 11:50:50 | 显示全部楼层
mark
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-10-17 00:08

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表