/// <summary> /// 获取类中属性类型的值 /// </summary> /// <param name="obj">Model对象</param> /// <param name="para">参数命令</param> /// <returns></returns> public static object GetProValue(object obj, string para) { Type Ts = obj.GetType(); string temp = para.Substring(para.IndexOf("@") + 1); //获得属性名称 string fieldValue = BuildSql.GetProValue(BuildSql.FirstUpper(temp), obj); //获的属性值 object v = Convert.ChangeType(fieldValue, Ts.GetProperty(BuildSql.FirstUpper(temp)).PropertyType); //转化为属性的类型 return(v); }
/// <summary> /// 生成实体类成员字段,不包括构造方法 /// </summary> /// <param name="table">数据表</param> /// <returns>字段名称</returns> public static string BuildModelField(string table) { string _model = ""; string txt1 = ""; string txt2 = ""; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand("select * from " + table, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); for (int i = 0; i < dr.FieldCount; i++) { string sny = dr.GetName(i); txt1 += "private " + dr.GetFieldType(i).ToString() + " " + sny + ";\r\n"; txt2 += "public " + dr.GetFieldType(i).ToString() + " " + BuildSql.FirstUpper(sny) + "\r\n{\r\n\t get \r\n\t{\r\n\t\t return this." + sny + ";\r\n\t}\r\n\t set \r\n\t{\r\n\t\t this." + sny + "=value;\r\n\t}\r\n}\r\n"; } txt1 += "\r\n"; _model += txt1 + txt2; return(_model); }