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