反射举例及其应用

1.类型参数约束

class A<T> where T:new()

  这是类型参数约束,where表明了对类型变量T的约束关系。where T:A 表示类型变量是继承于A的,或者是A本省。where T: new()指明了创建T的实例应该使用的构造函数。

  .NET支持的类型参数约束有以下五种:

  where T: struct                                      T必须是一个结构类型

  where T: class                                       T必须是一个类(class)类型,不是结构(structure)类型

  where T: struct()                                   T必须要为值类型

  where T: BaseClassName                       T必须继承名为BaseClassName的基类

  where T: IInterfaceName                       T必须实现名为NameOfInterface的接口

2.反射例子

(1)获取枚举中的描述特性值

如下代码将T限定为值类型,如:enum枚举。

1.获取T类型的所有公共成员,如:enum中所有枚举成员

2.循环T类型中所有公共成员进行操作

3.GetCustomAttributes为获取memInfo单个成员中自定义特性的数组,即:如果有多个特性,则将多个特性加入到attrs中

4.在attrs中描述特性默认为第一个也只有一个,将attrs[0]强制转为DescriptionAttribute

5.descriptionAttribute.Description为获取[Description("描述值")]中的“描述值”

        //用于返回当前枚举值中[Description("描述值")]特性的"描述值"        
        public static Dictionary<String, String> GetDescriptions<T>() where T : struct
        {
            Dictionary<String, String> result = new Dictionary<string, string>();            //获取T类型的所有公共成员,如:enum中所有枚举成员
            MemberInfo[] memInfos = typeof(T).GetMembers();
            //循环T类型中所有公共成员进行操作
            foreach (var memInfo in memInfos)
            { 
                //GetCustomAttributes为获取memInfo单个成员中自定义特性的数组,即:如果有多个特性,则将多个特性加入到attrs中
                object[] attrs = memInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs.Length > 0)
                {                  
                    //在本例中,描述特性默认为第一个也只有一个,强制转为DescriptionAttribute
                    var descriptionAttribute = attrs[0] as DescriptionAttribute;
                    if (descriptionAttribute != null)
                    {                            
                        //descriptionAttribute.Description为获取[Description("描述值")]中的“描述值”
                        result.Add(memInfo.Name, descriptionAttribute.Description);
                    }
                }
            }
            return result;
        }

(2)将DataTable转为IList

1.where T:new()在对T进行实例化(即:new())时,需要进行无参构造函数限定

2.PropertyInfo[]获取T的所有公共属性,与MenmberInfo[]类似

3.循环T类型中所有公共属性进行操作

4.根据Datatable中的方法dt.Columns.Contains对单个属性pi.Name进行筛选,如果列名有与之相同的名称则进行下一步操作

5.判断当前属性pi是否设置为可写,如果不可以则跳过该次循环,如果可以,则将当前Datatable行中列名与之相同的值赋给该属性

6.在Datatable的循环中把t加入到ts(IList<T>类)中

    public class ModelConvertHelper<T> where T : new()  // 此处一定要加上new()
    {
        public static IList<T> ConvertToModel(DataTable dt)
        {

            IList<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值