您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页C# 一些代码片段

C# 一些代码片段

来源:华佗小知识

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;
}

 

  

转载于:https://www.cnblogs.com/mango03/p/4423549.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务