C#基础泛型+反射的组合运用

内容:

如题。

目的:

需要完成两个例子。

1.利用反射加泛型来判断实体类的属性的值的情况

2.利用反射加泛型将DataTatale转换成List并判断List中集合的数据值

准备工作:

创建控制台程序;

创建一个Temp类;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Temp
    {
        public bool A { get; set; }
        public bool B { get; set; }
        public bool C { get; set; }
        public bool D { get; set; }
    }
}

因为例子比较基础所以方法没有再另外创类

开始编写

1.利用反射加泛型来判断实体类的属性的值的情况

现在有一个需求:需要判断Temp类的属性值里面是否存在False。

不用反射的方法编写是这样的:

if (temp.A && temp.B && temp.C && temp.D)
{
     return true;
}
else
{
     return false;
}

这样编写的耦合性太高,若字段很多或者需要添加字段时需要修改这段代码,不利于扩展

改成用反射和泛型的方法来编写

        public static bool IsAllTrue<T>(T obj) where T:class
        {
            Type t = typeof(T);  //创建一个T类型的类型
            PropertyInfo[] Proper = t.GetProperties();//通过反射,反射出T的所有属性
            foreach (var item in Proper)
            {
                bool bol = (bool)item.GetValue(obj,null);//获取属性的值
                if (!bol)
                {
                    return false;
                }
            }
            return true;
        }

调用方法

            Temp temp = new Temp()
            {
                A = true,
                B = true,
                C = false,
                D = true
            };
            Console.WriteLine(IsAllTrue<Temp>(temp).ToString());
            Console.Read();

这样编写的好处是:不会只局限于一个类,可以判断多个类,后期添加属性也不用再重新改代码,提高了代码的可扩展性

 

2.利用反射加泛型将DataTatale转换成List并判断List中集合的数据值

在实际开发中经常需要将DataTable转换成List或者List转换成dataTable

我们再编写一个泛型方法

public static IList<T> DataTableToList<T>(DataTable dt) where T : new()
        {
            //1.创建List集合
            IList<T> list = new List<T>();
            string TempName = "";
            foreach (DataRow ds in dt.Rows)
            {
                T t = new T(); //创建对象
                PropertyInfo[] Porp = t.GetType().GetProperties(); //获取对象的所有属性
                foreach (PropertyInfo item in Porp)
                {
                    TempName = item.Name;
                    if (dt.Columns.Contains(TempName))
                    {
                        if (!item.CanWrite) continue;
                        object value = ds[TempName]; //获取DataRow的值
                        if (value != DBNull.Value)
                        {
                            item.SetValue(t, value, null); //将值赋值给对象
                        }
                    }
                }
                list.Add(t);//填充List
            }
            return list;
        }

调用方法

            Temp temp = new Temp()
            {
                A = true,
                B = true,
                C = false,
                D = true
            };
            Console.WriteLine(IsAllTrue<Temp>(temp).ToString());

            DataTable dt = new DataTable();//创建DataTable
            dt.Columns.Add("A", typeof(bool));
            dt.Columns.Add("B", typeof(bool));
            dt.Columns.Add("C", typeof(bool));
            dt.Columns.Add("D", typeof(bool));
            dt.Rows.Add(new object[] { true, true, false, true });
            dt.Rows.Add(new object[] { true, true, true, true });
            dt.Rows.Add(new object[] { true, true, true, true });
            dt.Rows.Add(new object[] { true, true, false, true });
            IList<Temp> LTemp = DataTableToList<Temp>(dt);//利用刚写的方法转换成List
            int i = 1;
            foreach (Temp item in LTemp)
            {
                bool isbol = IsAllTrue<Temp>(item);//用上一个例子的方法来扩展第二个例子(实际开发中不需要这样编写,若是要判断值的话可以直接用DataTable的Rows进行判断,这里这样编写只是节目效果)
                if (!isbol)
                {
                    Console.WriteLine("第{0}行有值为False", i);
                }
                i++;
            }

            Console.Read();

总结:泛型和反射在C#编程中必不可少,上面的两个例子只是简单的使用泛型和反射,泛型和反射的作用还有很大,可以说反射和泛型在C#编程中 无处不在!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章