C#將部分Controls數(shù)據(jù)導(dǎo)入對象并存入ini中的操作方法
在日常的Winform設(shè)計工作中,將控件中的數(shù)據(jù)導(dǎo)出到對應(yīng)屬性或者字段中,再進行保存是經(jīng)常會用到的技巧;最簡單的就是同時遍歷控件和遍歷屬性字段進行名稱對比(需要保證控件的名稱要包含在字段屬性中);
本篇文章主要是在簡單的基礎(chǔ)上優(yōu)化整體邏輯,不僅僅是只遍歷控件和屬性進行名稱對比(適合類),還包含一些篩選;
1.遍歷控件和屬性得到控件的值
在下面代碼中,控件命名是以textBox_one的形式命名的
///類對象
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)成員屬性的信息以及提供對成員數(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;
//需要篩選對象屬性的類型
SetMemberValue(objectParm, item.Name, value);
//---------------------------------注意----------------------------------
//SetMemberValue函數(shù)是判斷屬性或者字段的類型,根據(jù)類型進行不同的賦值
}
}
else if (control is TextBox)
{
if (control.Name == TextName + item.Name)
{
string value = control.Text;
//需要篩選對象屬性的類型
SetMemberValue(objectParm,item.Name,value);
}
}
}
}
}
}2.利用FieldInfo的getSet函數(shù)設(shè)置類對象數(shù)據(jù)
/// <summary>
/// 設(shè)置類對象成員數(shù)據(jù)
/// </summary>
/// <param name="objectParm"></param>
/// <param name="fileName"></param>
/// <param name="filevalue"></param>
//Istype函數(shù)是對比類型是否一致
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"))
{
//判斷是否可以進行轉(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";//這個地方實際沒用到,在另外一個地方
[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"﹥項目名稱(如 [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"﹥項目名稱(如 [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.存入對象轉(zhuǎn)換為json存入ini
string Path = @"E:\ymx\Test\將部分Controls數(shù)據(jù)導(dǎo)入對象\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)入對象并存入ini中的文章就介紹到這了,更多相關(guān)C#Controls數(shù)據(jù)存入ini中內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 調(diào)用 JavaWebservice服務(wù)遇到的問題匯總
本文給大家分享的是個人在使用C#調(diào)用 JavaWebservice服務(wù)遇到的幾個問題的解決方法的匯總,給有類似需求的小伙伴們參考下吧。2016-01-01
WPF利用ValueConverter實現(xiàn)值轉(zhuǎn)換器
值轉(zhuǎn)換器在WPF開發(fā)中是非常常見的,值轉(zhuǎn)換器可以幫助我們很輕松地實現(xiàn),界面數(shù)據(jù)展示的問題。本文將通過WPF?ValueConverter實現(xiàn)簡單的值轉(zhuǎn)換器,希望對大家有所幫助2023-03-03
unity自帶尋路(導(dǎo)航)系統(tǒng) Nav Mesh導(dǎo)航網(wǎng)格
這篇文章主要為大家詳細介紹了unity自帶尋路(導(dǎo)航)系統(tǒng),Nav Mesh導(dǎo)航網(wǎng)格,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
C#利用Windows自帶gdi32.dll實現(xiàn)抓取屏幕功能實例
這篇文章主要介紹了C#利用Windows自帶gdi32.dll實現(xiàn)抓取屏幕功能,是C#程序設(shè)計中常見的一個重要技巧,需要的朋友可以參考下2014-08-08

