c# 将object尝试转为指定对象

 主方法:

 1         /// <summary>
 2         /// 将object尝试转为指定对象
 3         /// </summary>
 4         /// <param name="data"></param>
 5         /// <returns></returns>
 6         public static T ConvertObjToModel<T>(object data)
 7             where T : new()
 8         {
 9             if (data == null) return new T();
10             // 定义集合    
11             T result = new T();
12 
13             // 获得此模型的类型   
14             Type type = typeof(T);
15             string tempName = "";
16 
17             // 获得此模型的公共属性 
18             PropertyInfo[] propertys = result.GetType().GetProperties();
19             foreach (PropertyInfo pi in propertys)
20             {
21                 tempName = pi.Name;  // 检查object是否包含此列    
22 
23                 // 判断此属性是否有Setter      
24                 if (!pi.CanWrite) continue;
25 
26                 try
27                 {
28                     object value = GetPropertyValue(data, tempName);
29                     if (value != DBNull.Value)
30                     {
31                         Type tempType = pi.PropertyType;
32                         pi.SetValue(result, DealHelper.GetDataByType(value, tempType), null);
33 
34                     }
35                 }
36                 catch
37                 { }
38 
39             }
40 
41             return result;
42         }            

调用方法:

 1         /// <summary>
 2         /// 获取一个类指定的属性值
 3         /// </summary>
 4         /// <param name="info">object对象</param>
 5         /// <param name="field">属性名称</param>
 6         /// <returns></returns>
 7         public static object GetPropertyValue(object info, string field)
 8         {
 9             if (info == null) return null;
10             Type t = info.GetType();
11             IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
12             return property.First().GetValue(info, null);
13         }
14 
15 /// <summary>
16         /// 将数据转为制定类型
17         /// </summary>
18         /// <typeparam name="T"></typeparam>
19         /// <param name="data1"></param>
20         /// <returns></returns>
21         public static object GetDataByType(object data1, Type itype, params object[] myparams)
22         {
23             object result = new object();
24             try
25             {
26                 if (itype == typeof(decimal))
27                 {
28                     result = Convert.ToDecimal(data1);
29                     if (myparams.Length > 0)
30                     {
31                         result = Convert.ToDecimal(Math.Round(Convert.ToDecimal(data1), Convert.ToInt32(myparams[0])));
32                     }
33                 }
34                 else if (itype == typeof(double))
35                 {
36 
37                     if (myparams.Length > 0)
38                     {
39                         result = Convert.ToDouble(Math.Round(Convert.ToDouble(data1), Convert.ToInt32(myparams[0])));
40                     }
41                     else
42                     {
43                         result = double.Parse(Convert.ToDecimal(data1).ToString("0.00"));
44                     }
45                 }
46                 else if (itype == typeof(Int32))
47                 {
48                     result = Convert.ToInt32(data1);
49                 }
50                 else if (itype == typeof(DateTime))
51                 {
52                     result = Convert.ToDateTime(data1);
53                 }
54                 else if (itype == typeof(Guid))
55                 {
56                     result = new Guid(data1.ToString());
57                 }
58                 else if (itype == typeof(string))
59                 {
60                     result = data1.ToString();
61                 }
62             }
63             catch
64             {
65                 if (itype == typeof(decimal))
66                 {
67                     result = 0;
68                 }
69                 else if (itype == typeof(double))
70                 {
71                     result = 0;
72                 }
73                 else if (itype == typeof(Int32))
74                 {
75                     result = 0;
76                 }
77                 else if (itype == typeof(DateTime))
78                 {
79                     result = null;
80                 }
81                 else if (itype == typeof(Guid))
82                 {
83                     result = Guid.Empty;
84                 }
85                 else if (itype == typeof(string))
86                 {
87                     result = "";
88                 }
89             }
90             return result;
91         }
原文地址:https://www.cnblogs.com/King-JJ/p/10168805.html