把字符串当做变量名,给动态拼接的变量名赋值

最近工作中遇到了需要拼接变量名称,并对该变量进行赋值,一直觉得应该是动态变量名赋值,其实是反射,话不多说,

1.定义全局变量

 #region 1.图片初始化
        public  System.String img1 = "";
        public System.String img2 = "";
        public System.String img3 = "";
        public System.String img4 = "";
        public System.String img5 = "";
        public System.String img6 = "";
        public System.String img7 = "";
        public System.String img8 = "";
        public System.String img9 = "";
        public System.String img10 = "";
        public System.String img11 = "";
        public System.String img12 = "";
        #endregion

        #region 2.文字初始化
        public System.String lbtank1 = "";
        public System.String lbtype1 = "";
        public System.String lboh1 = "";
        public System.String lbcap1 = "";
        public System.String lbsh1 = "";
        public System.String lbtemp1 = "";

        public System.String lbtank2 = "";
        public System.String lbtype2 = "";
        public System.String lboh2 = "";
        public System.String lbcap2 = "";
        public System.String lbsh2 = "";
        public System.String lbtemp2 = "";

        public System.String lbtank3 = "";
        public System.String lbtype3 = "";
        public System.String lboh3 = "";
        public System.String lbcap3 = "";
        public System.String lbsh3 = "";
        public System.String lbtemp3 = "";

        public System.String lbtank4 = "";
        public System.String lbtype4 = "";
        public System.String lboh4 = "";
        public System.String lbcap4 = "";
        public System.String lbsh4 = "";
        public System.String lbtemp4 = "";

        public System.String lbtank5 = "";
        public System.String lbtype5 = "";
        public System.String lboh5 = "";
        public System.String lbcap5 = "";
        public System.String lbsh5 = "";
        public System.String lbtemp5 = "";

        public System.String lbtank6 = "";
        public System.String lbtype6 = "";
        public System.String lboh6 = "";
        public System.String lbcap6 = "";
        public System.String lbsh6 = "";
        public System.String lbtemp6 = "";

        public System.String lbtank7 = "";
        public System.String lbtype7 = "";
        public System.String lboh7 = "";
        public System.String lbcap7 = "";
        public System.String lbsh7 = "";
        public System.String lbtemp7 = "";

        public System.String lbtank8 = "";
        public System.String lbtype8 = "";
        public System.String lboh8 = "";
        public System.String lbcap8 = "";
        public System.String lbsh8 = "";
        public System.String lbtemp8 = "";

        public System.String lbtank9 = "";
        public System.String lbtype9 = "";
        public System.String lboh9 = "";
        public System.String lbcap9 = "";
        public System.String lbsh9 = "";
        public System.String lbtemp9 = "";

        public System.String lbtank10 = "";
        public System.String lbtype10 = "";
        public System.String lboh10 = "";
        public System.String lbcap10 = "";
        public System.String lbsh10 = "";
        public System.String lbtemp10 = "";

        public System.String lbtank11 = "";
        public System.String lbtype11 = "";
        public System.String lboh11 = "";
        public System.String lbcap11 = "";
        public System.String lbsh11 = "";
        public System.String lbtemp11 = "";

        public System.String lbtank12 = "";
        public System.String lbtype12 = "";
        public System.String lboh12 = "";
        public System.String lbcap12 = "";
        public System.String lbsh12 = "";
        public System.String lbtemp12 = "";
        #endregion

        List<String> imgs = new List<String> { "img","lbtank", "lbtype", "lboh", "lbcap", "lbsh", "lbtemp" };

2.利用反射给拼接的变量名赋值

 for (int i = 0; i < dt.Rows.Count; i++)
                {
                    List<String> ls = new List<String> { };
                    
                    string probe_num = dt.Rows[i]["probe_num"].ToString();
                    ls.Add(probe_num);
                    string oil_type = dt.Rows[i]["oil_type"].ToString();
                    ls.Add(oil_type);
                    string oil_high = dt.Rows[i]["oil_high"].ToString();
                    ls.Add(oil_high+"mm");
                    string pot_high = dt.Rows[i]["pot_high"].ToString();
                    string oil_capacity = dt.Rows[i]["oil_capacity"].ToString();
                    ls.Add(oil_capacity+"L");
                    string water_high = dt.Rows[i]["water_high"].ToString();
                    ls.Add(water_high+"mm");
                    string empty_capacity = dt.Rows[i]["empty_capacity"].ToString();
                    string temperature = dt.Rows[i]["temperature"].ToString();
                    ls.Add(temperature+"℃");
                    string color = dt.Rows[i]["color"].ToString();
                    string pama = probe_num + "," + oil_high + "," + pot_high + "," + oil_capacity + "," + water_high + "," + empty_capacity + "," + temperature + "," + oil_type + "," + color;
                    pama = TextEncrypt.Base64Encode(pama);
                    ls.Insert(0,"/handler/DrawImg.aspx?pama=" + pama);
                    for (int k = 0; k < imgs.Count; k++)
                    {
                        string div = imgs[k] + (i + 1).ToString();
                        FieldInfo fieldInfo = this.GetType().GetField(div);
                        fieldInfo.SetValue(this,(object) ls[k] );
                    }
                    
                }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值