字符串怎样实例化成对象

首先一看可能有点不明白,这个问题对于我们这些菜鸟来说确实挺神奇的,不过用完以后就没有什么感觉了,看看代码吧,

View Code
 1 string typename="Student";
 2 //实例化这个类型
 3 Type type = this.GetType().Assembly.GetType(typename, false, true);
 4 if (type != null) {
 5                 //取得类的无参数构造函数
 6                 var constructor = type.GetConstructor(new Type[0]);
 7                 //调用构造函数取得类的实例
 8                 var obj = constructor.Invoke(null);
 9                 //查找请求的方法
10                 var method = type.GetMethod(System.IO.Path.GetFileNameWithoutExtension(url));
11                 if (method != null) {
12                     //执行方法并输出响应结果
13                     object obj=method.Invoke(obj, null);
14                 }
15             }

这个便是实例化一个字符串的对象,当然首先要先定义好Student这个类。
上面的方法没有任何的参数,但是如果有参数该怎么办?

View Code
 1 Type type = this.GetType().Assembly.GetType(typename, false, true);
 2             if (type != null)
 3             {
 4                 //取得类的无参数构造函数
 5                 var constructor = type.GetConstructor(new Type[0]);
 6                 //调用构造函数取得类的实例
 7                 var obj = constructor.Invoke(null);
 8                 //查找请求的方法
 9                 var method = type.GetMethod(System.IO.Path.GetFileNameWithoutExtension(url));
10                 if (method != null)
11                 {
12                     var parameters = method.GetParameters();
13                     object[] args = null;
14                     if (parameters.Length > 0)
15                     {
16                         args = new object[parameters.Length];
17                         for (int x = 0; x < parameters.Length; x++)
18                         {
19                             args[x] = Convert.ChangeType(context.Request.Form[parameters[x].Name], parameters[x].ParameterType);
20                         }
21                     }
22                     //执行方法并输出响应结果
23                     context.Response.Write(method.Invoke(obj, args));
24                 }
25             }

还有一种情况,如果已经知道一个类,比如Student,还有一个属性Name,但是这个属性是个字符串,有可能是别的地方传过来的但是我要取得这个属性的值,代码如下:

View Code
1 PropertyInfo p = typeof(Student).GetProperty("Name");
2 string strValue= p.GetValue(new Student(), null).ToString();

其实这种情况可以在很多地方都可以用到比如怎么样将DataSet转换成List<类>等等。代码如下:

View Code
 1 /// <summary>
 2         /// Dataset转换成List
 3         /// </summary>
 4         /// <typeparam name="T">实体</typeparam>
 5         /// <param name="p_DataSet">Dataset数据集</param>
 6         /// <param name="p_TableIndex">表序号(一般一个DataSet中只有一个Table所以序号为0)</param>
 7         /// <returns>List集合</returns>
 8         public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
 9         {
10             if (p_DataSet == null || p_DataSet.Tables.Count < 0)
11                 return null;
12             if (p_TableIndex > p_DataSet.Tables.Count - 1)
13                 return null;
14             if (p_TableIndex < 0)
15                 p_TableIndex = 0; DataTable p_Data = p_DataSet.Tables[p_TableIndex];
16             IList<T> result = new List<T>();
17             for (int j = 0; j < p_Data.Rows.Count; j++)
18             {
19                 //实例化对象
20                 T _t = (T)Activator.CreateInstance(typeof(T));
21                 //取出对象中的属性
22                 PropertyInfo[] propertys = _t.GetType().GetProperties();
23                 foreach (PropertyInfo pi in propertys)
24                 {
25                     for (int i = 0; i < p_Data.Columns.Count; i++)
26                     {
27                         // 属性与字段名称一致的进行赋值                     
28                         if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
29                         {
30                             // 数据库NULL值单独处理                           
31                             if (p_Data.Rows[j][i] != DBNull.Value)
32                                 //给对应的属性赋值
33                                 pi.SetValue(_t, p_Data.Rows[j][i], null);
34                             else
35                                 pi.SetValue(_t, null, null);
36                             break;
37                         }
38                     }
39                 }
40                 result.Add(_t);
41             }
42             return result;
43         }

 

 

posted @ 2012-06-29 17:44  独孤雄  阅读(514)  评论(0编辑  收藏  举报