C# 一些代码片段
2015-04-13 22:37
211 查看
1、获取集合里泛型类型的实例对象
List<T> list; Type[] types = list.GetType().GetGenericArguments(); foreach(var type in types) { System.Activator.CreateInstance(type); } 虽然是个数组,但是此处好像只有一个值,所以 object obj =System.Activator.CreateInstance(types.First());
//然后可以获取类的属性做一些操作 var properties = TypeDescriptor.GetProperties(obj); for (var i = 0; i < properties.Count; i++) { var property = properties[i]; //如果字段有 [Display(Name = "手机")] 或 [DisplayName("姓名")] 特性,则优先显示DisplayName,如果都为空则显示字段名 var displayAttribute = property.Attributes[typeof(DisplayAttribute)] as DisplayAttribute; string columnName = property.DisplayName == property.Name ? (displayAttribute == null ? property.Name : displayAttribute.Name) : property.DisplayName; }
相关文章推荐
- 与Pocket Outlook相关的一些代码片段(C#)
- 与Pocket Outlook相关的一些代码片段(C#)
- 一些有关图像处理的代码片段(抓图、倒影、圆角)
- 一些有关图像处理的代码片段(抓图、倒影、圆角)
- C# Winform代码片段-大二下学期的垃圾代码
- 10个C#程序员经常用到的实用代码片段
- C#程序员经常用到的10个实用代码片段
- 代码片段 - C#基于google api计算距离
- C#程序员经常用到的10个实用代码片段
- TreeView(C#)无限目录树代码片段
- 收藏的25个非常有用的CSS代码片段,在你开发基于CSS的网站时,这些代码是经常用到的,比方说一些特殊的效果,圆角边框,CSS透明度,梯形环绕,CSS小三角等,希望对你有用
- C# Winform代码片段-大二下学期的垃圾代码
- 一些很有用的 PHP 代码片段
- C#程序员经常用到的10个实用代码片段
- c#代码的一些模式
- [转]c#有关winform的两个代码片段(多线程操作窗体控件与窗体淡入淡出效果)
- wxWidgets一些代码片段(不断更新中)
- 一些实用的jQuery代码片段收集
- Android开发中一些直接拿来用的代码片段