欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#將部分Controls數(shù)據(jù)導(dǎo)入對(duì)象并存入ini中的操作方法

 更新時(shí)間:2024年10月09日 12:35:23   作者:薄荷撞~可樂  
在Winform設(shè)計(jì)中,經(jīng)常需要將控件數(shù)據(jù)導(dǎo)出到屬性或字段中,本文詳細(xì)介紹了如何優(yōu)化這一過程,包括控件和屬性的遍歷,以及使用FieldInfo的getSet函數(shù)和Ini類庫來實(shí)現(xiàn)數(shù)據(jù)的有效存儲(chǔ)和轉(zhuǎn)換,感興趣的朋友跟隨小編一起看看吧

在日常的Winform設(shè)計(jì)工作中,將控件中的數(shù)據(jù)導(dǎo)出到對(duì)應(yīng)屬性或者字段中,再進(jìn)行保存是經(jīng)常會(huì)用到的技巧;最簡單的就是同時(shí)遍歷控件和遍歷屬性字段進(jìn)行名稱對(duì)比(需要保證控件的名稱要包含在字段屬性中);

本篇文章主要是在簡單的基礎(chǔ)上優(yōu)化整體邏輯,不僅僅是只遍歷控件和屬性進(jìn)行名稱對(duì)比(適合類),還包含一些篩選;

1.遍歷控件和屬性得到控件的值

 在下面代碼中,控件命名是以textBox_one的形式命名的

///類對(duì)象
class ObjectParm
    {
        public int one;
        public string two;
        public int three;
        public string four;
        public int five;
        public string six;
    }
private void Save(ObjectParm objectParm, Control controls, string TextName = "textBox_", string ComboBoxName = "comboBox_")
        {
            Type type = objectParm.GetType();
            //獲取有關(guān)成員屬性的信息以及提供對(duì)成員數(shù)據(jù)的訪問
            MemberInfo[] memberInfos = type.GetMembers();//獲取所有公共成員
            foreach (Control control in controls.Controls)
            {
                foreach (MemberInfo item in memberInfos)
                {
                    //這里字段屬性均可以
                    if (item.MemberType == MemberTypes.Field)
                    {
                        if (control is ComboBox)
                        {
                            if (control.Name == ComboBoxName + item.Name)
                            {
                                string value = control.Text;
                                //需要篩選對(duì)象屬性的類型
                                SetMemberValue(objectParm, item.Name, value);
//---------------------------------注意----------------------------------
//SetMemberValue函數(shù)是判斷屬性或者字段的類型,根據(jù)類型進(jìn)行不同的賦值
                            }
                        }
                        else if (control is TextBox)
                        {
                            if (control.Name == TextName + item.Name)
                            {
                                string value = control.Text;
                                //需要篩選對(duì)象屬性的類型
                                SetMemberValue(objectParm,item.Name,value);
                            }
                        }
                    }
                }
            }
        }

 2.利用FieldInfo的getSet函數(shù)設(shè)置類對(duì)象數(shù)據(jù)

/// <summary>
        /// 設(shè)置類對(duì)象成員數(shù)據(jù)
        /// </summary>
        /// <param name="objectParm"></param>
        /// <param name="fileName"></param>
        /// <param name="filevalue"></param>
//Istype函數(shù)是對(duì)比類型是否一致
        private bool SetMemberValue(ObjectParm objectParm, string fileName, string filevalue)
        {
            Type type = objectParm.GetType();
            //發(fā)現(xiàn)字段屬性并提供訪問
            FieldInfo fieldInfo = type.GetField(fileName);//搜索字段
            bool ConverFlag = true;
            //類型匹配
            if (Istype(fieldInfo.FieldType, "System.String"))
            {
                fieldInfo.SetValue(objectParm, filevalue);
            }
            if (Istype(fieldInfo.FieldType, "System.Double"))
            {
                //判斷是否可以進(jìn)行轉(zhuǎn)
                double result = 0;
                if (!double.TryParse(filevalue, out result)) ConverFlag = false;
                fieldInfo.SetValue(objectParm, result);
            }
            if (Istype(fieldInfo.FieldType, "System.Single"))
            {
                float result = 0;
                if (!float.TryParse(fileName, out result))
                    ConverFlag = false;
                fieldInfo.SetValue(objectParm, result);
            }
            if (Istype(fieldInfo.FieldType, "System.Boolean"))
            {
                bool flag = false;
                if (!Boolean.TryParse(fileName, out flag))
                    ConverFlag = false;
                fieldInfo.SetValue(objectParm, flag);
            }
            if (Istype(fieldInfo.FieldType, "System.UInt32"))
            {
                uint value = 0;
                if (!uint.TryParse(fileName, out value))
                    ConverFlag = false;
                fieldInfo.SetValue(objectParm, value);
            }
            if (Istype(fieldInfo.FieldType, "System.UInt16"))
            {
                UInt16 value = 0;
                if (!UInt16.TryParse(fileName, out value))
                    ConverFlag = false;
                fieldInfo.SetValue(objectParm, value);
            }
            if (Istype(fieldInfo.FieldType, "System.Int32"))
            {
                int value = 0;
                if (!Int32.TryParse(fileName, out value))
                    ConverFlag = false;
                fieldInfo.SetValue(objectParm, value);
            }
            if (Istype(fieldInfo.FieldType, "System.Decimal"))
            {
                if (filevalue != "")
                    fieldInfo.SetValue(objectParm, Decimal.Parse(filevalue));
                else
                    fieldInfo.SetValue(objectParm, new Decimal(0));
            }
            if (Istype(fieldInfo.FieldType, "System.Nullable`1[System.DateTime]"))
            {
                if (filevalue != "")
                {
                    try
                    {
                        fieldInfo.SetValue(objectParm, (DateTime?)DateTime.ParseExact(filevalue, "yyyy-MM-dd HH:mm:ss", null));
                    }
                    catch
                    {
                        fieldInfo.SetValue(objectParm, (DateTime?)DateTime.ParseExact(filevalue, "yyyy-MM-dd", null));
                    }
                }
                else
                    fieldInfo.SetValue(objectParm, null);
            }
            return ConverFlag;
        }
private bool Istype(Type type, string typeName)
        {
            if (type.ToString() == typeName)
            { return true; }
            if (type.ToString() == "System.Object")
                return false;
            return Istype(type.BaseType, typeName);
        }

 3.Ini簡易類庫編寫

class IniClass
    {
        public static string inipath = Directory.GetCurrentDirectory() + "\\" + "systemset.ini";//這個(gè)地方實(shí)際沒用到,在另外一個(gè)地方
        [System.Runtime.InteropServices.DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [System.Runtime.InteropServices.DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        public IniClass(string inipath_)
        {
            inipath = inipath_;
        }
        /// ﹤summary﹥   /// 寫入INI文件   /// ﹤/summary﹥   /
        // ﹤param name="Section"﹥項(xiàng)目名稱(如 [TypeName] )﹤/param﹥   
        /// ﹤param name="Key"﹥鍵﹤/param﹥   
        /// ﹤param name="Value"﹥值﹤/param﹥   
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, inipath);
        }
        /// ﹤summary﹥   
        /// 讀出INI文件   
        /// ﹤/summary﹥   
        /// ﹤param name="Section"﹥項(xiàng)目名稱(如 [TypeName] )﹤/param﹥   
        /// ﹤param name="Key"﹥鍵﹤/param﹥   
        public string IniReadValue(string Section, string Key, string default_value = "")
        {
            StringBuilder temp = new StringBuilder(50000);
            int i = GetPrivateProfileString(Section, Key, default_value, temp, 50000, inipath);
            return temp.ToString();
        }

 4.存入對(duì)象轉(zhuǎn)換為json存入ini

string Path = @"E:\ymx\Test\將部分Controls數(shù)據(jù)導(dǎo)入對(duì)象\sys.ini";
        private void button1_Click(object sender, EventArgs e)
        {
            Save(objectParm,panel1, "textBox_", "comboBox_");
            string str = JsonConvert.SerializeObject(objectParm);
            //存入ini
            IniClass iniClass = new IniClass(Path);
            iniClass.IniWriteValue("Parm", "trest", str);
         }

 5.效果展示

到此這篇關(guān)于C#將部分Controls數(shù)據(jù)導(dǎo)入對(duì)象并存入ini中的文章就介紹到這了,更多相關(guān)C#Controls數(shù)據(jù)存入ini中內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論