public IList<object> xx(DataTable myDt, object targetObj)
    {        System.Reflection.PropertyInfo[] ps = targetObj.GetType().GetProperties();
        IList<object> list = new List<object>();
        foreach (DataRow row in myDt.Rows)
        {
            foreach (System.Reflection.PropertyInfo p in ps)
            {
                if (row[p.Name] != null)
                {
                    p.SetValue(targetObj, row[p.Name], null);
                }
            }
            list.Add(ps);
            
        }        return list;
    }  我想写个通用的反射 ` `但学的年代太久了  各位帮忙完善下 谢谢` 

解决方案 »

  1.   


            public bool InsertEntity(IEntity entity)
            {
                SqlConnection conn = null;
                try
                {
                    Type type = entity.GetType();
                    PropertyInfo[] list = type.GetProperties();
                    conn = GetSqlConnection();
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;                //生成sql语句
                    StringBuilder sql = new StringBuilder();
                    StringBuilder sqlValue = new StringBuilder();
                    sql.Append("insert into " + type.Name + "(");
                    sqlValue.Append(" values(");
                    string keyName = this.GetKeyName(type.Name);
                    foreach (PropertyInfo info in list)
                    {
                        if (info.Name == keyName ||
                            info.GetValue(entity, null) == null)
                        {
                            continue;
                        }
                        sql.Append("[" + info.Name + "],");
                        sqlValue.Append("@" + info.Name + ",");
                        SqlParameter p = new SqlParameter();
                        p.ParameterName = "@" + info.Name;
                        p.Value = info.GetValue(entity, null);
                        cmd.Parameters.Add(p);
                    }
                    string sqlGetId = "select max(" + keyName + ") from " + type.Name;
                    string cmdText = sql.ToString().TrimEnd(new char[] { ',' }) + ")" +
                        sqlValue.ToString().TrimEnd(new char[] { ',' }) + ");" + sqlGetId;
                    cmd.CommandText = cmdText;                //找主键
                    PropertyInfo pi = null;
                    foreach (PropertyInfo info in list)
                    {
                        if (info.Name == keyName)
                        {
                            pi = info;
                            break;
                        }
                    }                //执行并给ID赋值
                    lock (_lock)
                    {
                        object id = cmd.ExecuteScalar();
                        pi.SetValue(entity, id, null);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
      

  2.   

    IList<object>?把你的object删掉!
      

  3.   

    public object xx(DataTable myDt,Type t)
    {    System.Reflection.PropertyInfo[] ps = t.GetProperties();
        object obj = t.Assembly.CreateInstance(t.FullName);
        foreach (DataRow row in myDt.Rows)
        {
            foreach (System.Reflection.PropertyInfo p in ps)
            {
                if (row[p.Name] != null)
                {
                    p.SetValue(obj, row[p.Name], null);
                }
            }
        }
        return obj;
    }
      

  4.   


     public object GetEntity(DataRow row, object targetObj)
        {
            System.Reflection.PropertyInfo[] ps = targetObj.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo p in ps)
            {
                if (row[p.Name] is DBNull)
                {
                   
                }
                else
                {
                    p.SetValue(targetObj, row[p.Name], null);
                }        }        return targetObj;
        }
      

  5.   


    if (row[p.Name] != null)
     
       这样判断 如果INT32类型的变量数据库的值为NULL 会出错` 
      

  6.   

    至于如何判断那是你的事了,如果是null你可以多加判断