1. 程式人生 > >用反射實現為物件的屬性賦值及使用泛型建立例項

用反射實現為物件的屬性賦值及使用泛型建立例項

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

namespace DataMapperTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //0. 準備待轉換的資料
            Hashtable ht = new Hashtable();
            ht.Add("UserId", 1);
            ht.Add("UserName","小明1");
            ht.Add("Remark", "厲害了我的國");
            ht.Add("UserType", 2);
            ht.Add("InsertTime", DateTime.Parse( "2018-05-12"));
            ht.Add("Score", 81.5);
            ht.Add("Balance", (decimal)302.25);

            UserInfo user = new UserInfo();
            Stopwatch sw = Stopwatch.StartNew();
            Type type = user.GetType(); //獲取型別
            PropertyInfo[] propertyInfoList = type.GetProperties(); //獲取指定名稱的所有屬性
            //Console.WriteLine("------------ 下面是修改前: ------------");
            foreach (PropertyInfo prop in propertyInfoList)
            {
                //Console.WriteLine("{0} : {1} ", prop.Name, prop.GetValue(user));
                prop.SetValue(user, ht[prop.Name]); //設定值
            }
            Console.WriteLine("ElapsedMilliseconds : {0} ", sw.ElapsedMilliseconds);

            Console.WriteLine("------------ 下面是修改後: ------------");

            //輸出賦值後的物件屬性值
            PrintInfo(user);
            Console.Read();
        }

        public static void PrintInfo(UserInfo item)
        {
            Console.WriteLine("{0} : {1} ", "UserId", item.UserId);
            Console.WriteLine("{0} : {1} ", "UserName", item.UserName);
            Console.WriteLine("{0} : {1} ", "Remark", item.Remark);
            Console.WriteLine("{0} : {1} ", "UserType", item.UserType);
            Console.WriteLine("{0} : {1} ", "InsertTime", item.InsertTime);
            Console.WriteLine("{0} : {1} ", "Score", item.Score);
            Console.WriteLine("{0} : {1} ", "Balance", item.Balance);

            Console.WriteLine();
        }
    }

    public class UserInfo
    {
        public long UserId { get; set; }
        public string UserName { get; set; }
        public string Remark { get; set; }
        public int UserType { get; set; }
        public DateTime InsertTime { get; set; }
        public double Score { get; set; }
        public decimal Balance { get; set; }
    }
}

效率比想象中要高。 直接執行(release)一般 0-3 ms,在dos 下執行 .exe 檔案都是 0 ms.

類似上面, 改為 DataRow 為 物件賦值也很容易。

下面改為泛型,讓程式碼更加通用:

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

namespace DataMapperTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //0. 準備待轉換的資料
            Hashtable ht = new Hashtable();
            ht.Add("UserId", 1);
            ht.Add("UserName","小明1");
            ht.Add("Remark", "厲害了我的國");
            ht.Add("UserType", 2);
            ht.Add("InsertTime", DateTime.Parse( "2018-05-12"));
            ht.Add("Score", 81.5);
            ht.Add("Balance", (decimal)302.25);

            Stopwatch sw = Stopwatch.StartNew();
            UserInfo user = SetValue<UserInfo>( ht);
            Console.WriteLine("ElapsedMilliseconds : {0} ", sw.ElapsedMilliseconds);

            Console.WriteLine("------------ 下面是修改後: ------------");

            //輸出賦值後的物件屬性值
            PrintInfo(user);
            Console.Read();
        }

        public static T SetValue<T>(Hashtable ht)
        {
            T item = System.Activator.CreateInstance<T>();
            Type type = item.GetType();
            PropertyInfo[] propertyInfoList = type.GetProperties(); //獲取指定名稱的所有屬性
            foreach (PropertyInfo prop in propertyInfoList)
            {
                prop.SetValue(item, ht[prop.Name]); //設定值
            }
            return item;
        }

        public static void PrintInfo(UserInfo item)
        {
            Console.WriteLine("{0} : {1} ", "UserId", item.UserId);
            Console.WriteLine("{0} : {1} ", "UserName", item.UserName);
            Console.WriteLine("{0} : {1} ", "Remark", item.Remark);
            Console.WriteLine("{0} : {1} ", "UserType", item.UserType);
            Console.WriteLine("{0} : {1} ", "InsertTime", item.InsertTime);
            Console.WriteLine("{0} : {1} ", "Score", item.Score);
            Console.WriteLine("{0} : {1} ", "Balance", item.Balance);

            Console.WriteLine();
        }
    }

    public class UserInfo
    {
        public long UserId { get; set; }
        public string UserName { get; set; }
        public string Remark { get; set; }
        public int UserType { get; set; }
        public DateTime InsertTime { get; set; }
        public double Score { get; set; }
        public decimal Balance { get; set; }
    }
}