日期:2014-05-17  浏览次数:20851 次

类定义中,批量修改当前对像的的属性
User 类 成员变量中定义了很多属性userid,usename,mail,mobile,等等
定义了实例化成员函数
User (int uid)
{
 this.userid = uid;
}
但其它属性是空的,定义无返回值的成员函数
Get()
{
//从数据库用LINQ查询到一个对应的实例物像,想把查询到的实例对象的对应的属性值赋给当前对像
 var res = from rs in db.User where rs.UserId == this.UserId  select rs;
          if (res.Count() > 0)
          {
              User z = res.First();
              //但是不能用this = z;
              //也不想一个个的用属性名来复制如 this.userid = z.userid;因为成员变量比较多
              //求助:能不能用反射或其它的方式达到我的目的  类成员函数对于当前对像不太会用反射,
          }
}

万分感谢
------解决方案--------------------

    /// <summary>
    /// 类定义
    /// </summary>
    public class User
    {
        public int userid { get; set; }
        public string username { get; set; }
        public string mail { get; set; }
        public string mobile { get; set; }

        public User(int userid)
        {
            this.userid = userid;
        }

        public void Get()
        {
            using (BirthdayDataClassesDataContext db = new BirthdayDataClassesDataContext())
            {
                TableA result = (from u in db.TableA
                                where u.userid == this.userid
                                select u).FirstOrDefault();
                if (result != null)
                {
                    PropertyInfo[] pros = typeof(TableA).GetProperties();

                    fore