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#編程中 無處不在!

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