使用反射遍历对象属性置取值

protected void RoleSearch(string strPlayerID, string strServerID, string strRoleName)
    {
        try
        {
            SrvRefs.OperationLog(string.Format("角色信息查询:[IP:{0}]", StringUtils.GetClientIP()));
            CPlayerInfo PlayerInfo;
            string strErrInfo;
            CCMGameInterface gi = new CCMGameInterface(ushort.Parse(strServerID), (ushort)1001);
            int iRetCode = gi.QueryPlayerInfo(ulong.Parse(strPlayerID), out PlayerInfo, out strErrInfo);
            if (iRetCode == 1)
            {
                lab_RoleName.Text = strRoleName;
                lab_Level.Text = PlayerInfo.Level.ToString();
                lab_Career.Text = PlayerInfo.Career.ToString();
                lab_HP.Text = PlayerInfo.HP.ToString();
                lab_MP.Text = PlayerInfo.MP.ToString();
                lab_Block.Text = PlayerInfo.Block.ToString();
                lab_Money.Text = PlayerInfo.Money.ToString();
                lab_PhyDef.Text = PlayerInfo.PhyDef.ToString();
                lab_PhyCritical.Text = PlayerInfo.PhyCritical.ToString();
                lab_PastLevel.Text = PlayerInfo.PastLevel.ToString();
                lab_ShiftTimes.Text = PlayerInfo.SoulShiftTimes.ToString();
                tab_PlayerInfo.Visible = true;

                DataTable dt = new DataTable();
                dt.Columns.Add("属性名称",typeof(string));
                dt.Columns.Add("属性值", typeof(string));
                DataRow dr = null;
                System.Reflection.PropertyInfo[] ps = PlayerInfo.GetType().GetProperties();
                foreach (System.Reflection.PropertyInfo pi in ps)
                {
                    dr = dt.NewRow();
                    string name = pi.Name;
                    string value = Convert.ToString(pi.GetValue(PlayerInfo, null));
                    dr["属性名称"] = name;
                    dr["属性值"] = value;
                    dt.Rows.Add(dr);
                }
                gv_Roles.DataSource = dt;
                gv_Roles.DataBind();
            }
            else
            {
                MsgBoxs.MsgBox(this, strErrInfo);
            }
        }
        catch (Exception ex)
        {
            LogManager.WriteTextLog("GM", "Role_RoleSearch", "btn_RoleSearch_Click", ex.Message);
        }
    }

使用反射遍历对象属性置取值
        PropertyInfo[] Properties = gu.GetType().GetProperties();
 2            int currentRow = 0;
 3            int currentColumn = 0;
 4            foreach (PropertyInfo pi in Properties)
 5            {
 6                Label lbl = new Label();
 7                lbl.Text = string.Format("{0}:", pi.Name);
 8                tlp.Controls.Add(lbl);
 9                tlp.SetCellPosition(lbl, new TableLayoutPanelCellPosition(currentColumn, currentRow));
10
11                TextBox tb = new TextBox();
12                tb.ReadOnly = !alUserPropertiesNecessary.Contains(pi.Name.ToLower());
13
14                try
15                {
16                    tb.Text = pi.GetValue(gu, null).ToString();
17                }
18                catch
19                {
20                    tb.Text = string.Empty;
21                }
22                tlp.Controls.Add(tb);
23                tlp.SetCellPosition(tb, new TableLayoutPanelCellPosition(currentColumn + 1, currentRow));
24                currentRow++;
25                if ((currentRow % 11).Equals(0))
26                {
27                    currentColumn += 2;
28                    currentRow = 0;
29                }
30                tb.Name = pi.Name.ToLower();// +"R";
31            }
     置值
1            GlobalUser gu = new GlobalUser();
2            PropertyInfo[] Properties = gu.GetType().GetProperties();
3
4            foreach (PropertyInfo pi in Properties)
5            {
6                tb = (TextBox)tlp.Controls.Find(pi.Name.ToLower(), true)[0];
7                pi.SetValue(gu, Convert.ChangeType(tb.Text, pi.PropertyType), null);
8            }
9            return gu;
0 0 0
(请您对文章做出评价)

你可能感兴趣的:(使用反射遍历对象属性置取值)