void mFileChangedHandler(object sender, FileSystemEventArgs e) { try { if (!String.IsNullOrEmpty(FileName)) { if (e.Name.Equals(Path.GetFileName(FileName))) { this.Data = IniReader.Deserialize <T>(FileName); } } } catch (Exception ee) { } }
public static void Test() { //可以從字串Deserialize到物件 ExampleClass example = IniReader.DeserializeString <ExampleClass>( "StringField=Hello World" + Environment.NewLine + "IntField=1234567890" + Environment.NewLine + "DoubleField=3.1415926" + Environment.NewLine + "SizeField=100,100" + Environment.NewLine + "RectangleField=1,2,3,4" + Environment.NewLine + "PointField=5,6" + Environment.NewLine + "InnerClassField.StringField=InnerClass String" + Environment.NewLine + "InnerClassField.IntField=1000" + Environment.NewLine + "InnerClassField.IntField2=2000" + Environment.NewLine + "InnerClassField.DoubleField=3000" + Environment.NewLine ); }
public Form BuildForm(String title = "") { UI.SaveConfigurationTemplateForm ret = new UI.SaveConfigurationTemplateForm(); DataFieldControl.Clear(); ToolTip tip = new ToolTip(); this.Value = IniReader.Deserialize <T>(this.TargetFileName, HandleDeserializeField); List <GroupBox> groupBoxs = new List <GroupBox>(); if (String.IsNullOrEmpty(title)) { title = this.TargetFileName; } ret.Text = title; ret.TipText = "設定(點選欄位/標籤可以看到欄位敘述)"; foreach (String g in FieldCategoryMap.Keys) { GroupBox gbox = new GroupBox(); groupBoxs.Add(gbox); gbox.Margin = new Padding(3); List <String> names = FieldCategoryMap[g]; TableLayoutPanel tablelayout = new TableLayoutPanel(); tablelayout.RowCount = names.Count; tablelayout.ColumnCount = 2; tablelayout.AutoScroll = true; for (int i = 0; i < 2; ++i) { ColumnStyle rs = new ColumnStyle(); tablelayout.ColumnStyles.Add(rs); tablelayout.ColumnStyles[i].SizeType = SizeType.Percent; tablelayout.ColumnStyles[i].Width = 0.5f; } if (String.IsNullOrEmpty(g)) { gbox.Text = "Default"; } else { gbox.Text = g; } int row = 0; foreach (String name in names) { IniReader.OnSerializeNotificationEventArgs arg = FieldDeserializeMap[name]; if (arg.Field.FieldType.IsClass) { if (arg.Field.FieldType != typeof(String) && arg.Field.FieldType != typeof(Rectangle) && arg.Field.FieldType != typeof(Size) && arg.Field.FieldType != typeof(Point) && arg.Field.FieldType != typeof(Color) && arg.Field.FieldType != typeof(int[])) { continue; } } FieldDisplayNameAttribute displayName = (FieldDisplayNameAttribute)arg.Field.GetCustomAttribute(typeof(FieldDisplayNameAttribute), true); DescriptionAttribute description = (DescriptionAttribute)arg.Field.GetCustomAttribute(typeof(DescriptionAttribute), true); CategoryAttribute category = (CategoryAttribute)arg.Field.GetCustomAttribute(typeof(CategoryAttribute), true); AttributeConfigureUIVisible visible = (AttributeConfigureUIVisible)arg.Field.GetCustomAttribute(typeof(AttributeConfigureUIVisible), true); RowStyle rs = new RowStyle(); if (ShowAll || visible == null || visible.Visible) { tablelayout.RowStyles.Add(rs); rs.SizeType = SizeType.Absolute; rs.Height = 30; } String descriptionTxt = ""; if (description != null) { descriptionTxt = description.Description; } else { descriptionTxt = name; } EventHandler focusHandler = new EventHandler((object s, EventArgs e) => { ret.TipText = descriptionTxt; }); Panel left = new Panel(); Panel right = new Panel(); left.Dock = DockStyle.Fill; right.Dock = DockStyle.Fill; left.Margin = new Padding(0); right.Margin = new Padding(0); Label nameLabel = new Label(); String strname = GetPropertyDisplayName(arg.Field); if (String.IsNullOrEmpty(strname)) { strname = arg.FullName; } if (displayName != null) { nameLabel.Text = String.Format("{0}({1})", displayName.DisplayName, strname); } else { nameLabel.Text = strname; } nameLabel.Dock = DockStyle.Fill; nameLabel.TextAlign = System.Drawing.ContentAlignment.TopRight; nameLabel.Click += focusHandler; left.Controls.Add(nameLabel); IniReader reader = arg.Reader; Type fieldType = arg.Field.FieldType; bool customEditorHandled = false; if (OnCustomEditorNeeded != null) { CustomFormEditEventArg args = new CustomFormEditEventArg(); args.FieldName = name; args.DisplayName = strname; args.Description = descriptionTxt; args.Value.Value = arg.FieldValue; args.SerializeArg = arg; OnCustomEditorNeeded(this, args); if (args.Handled) { if (args.CustomControl != null) { Control ctrl = args.CustomControl; ctrl.Size = new Size(100, (int)(rs.Height)); ctrl.Dock = DockStyle.Fill; ctrl.Tag = arg.FullName; ctrl.GotFocus += focusHandler; right.Controls.Add(ctrl); customEditorHandled = true; } } } if (!customEditorHandled) { if (fieldType == typeof(Color)) { Button btn = new Button(); btn.Size = new Size(100, (int)(rs.Height)); btn.BackColor = (Color)arg.FieldValue; btn.Dock = DockStyle.Fill; btn.Click += btn_colorPickerClick; right.Controls.Add(btn); btn.Tag = arg.FullName; btn.GotFocus += focusHandler; this.DataFieldControl.Add(btn); } else if (fieldType == typeof(bool)) { ComboBox cbox = new ComboBox(); cbox.DropDownStyle = ComboBoxStyle.DropDownList; cbox.Items.Add("false"); cbox.Items.Add("true"); cbox.Dock = DockStyle.Fill; cbox.Text = (String)arg.FieldValue.ToString().ToLower(); right.Controls.Add(cbox); cbox.Tag = arg.FullName; cbox.GotFocus += focusHandler; this.DataFieldControl.Add(cbox); } else { if (fieldType.IsEnum) { ComboBox cbox = new ComboBox(); cbox.DropDownStyle = ComboBoxStyle.DropDownList; var enumNames = fieldType.GetEnumNames(); cbox.Items.AddRange(enumNames); cbox.Dock = DockStyle.Fill; String val = ""; if (arg.FieldValue != null) { val = arg.FieldValue.ToString(); } cbox.Text = val; right.Controls.Add(cbox); cbox.Tag = arg.FullName; cbox.GotFocus += focusHandler; this.DataFieldControl.Add(cbox); } else { TextBoxEx tbox = new TextBoxEx(); tbox.Size = new Size(100, (int)(rs.Height)); tbox.Dock = DockStyle.Fill; tbox.IsChangeTracked = true; right.Controls.Add(tbox); tbox.Tag = arg.FullName; tbox.Text = (String)arg.FieldValue.ToString(); tbox.GotFocus += focusHandler; tbox.Click += (s, e) => { if (OnCustomForm != null) { CustomFormEditEventArg args = new CustomFormEditEventArg(); args.FieldName = name; args.DisplayName = strname; args.Description = descriptionTxt; args.Value = tbox.Text; args.SerializeArg = arg; OnCustomForm(this, args); if (args.Handled) { tbox.Text = args.Value.ToString(); } } }; this.DataFieldControl.Add(tbox); } } } if (ShowAll || visible == null || visible.Visible) { tablelayout.Height += ((int)(rs.Height)); tablelayout.Controls.Add(left); tablelayout.Controls.Add(right); tablelayout.SetCellPosition(left, new TableLayoutPanelCellPosition(0, row)); tablelayout.SetCellPosition(right, new TableLayoutPanelCellPosition(1, row)); ++row; } } tablelayout.Dock = DockStyle.Fill; gbox.Height = tablelayout.Height; gbox.Controls.Add(tablelayout); } foreach (var g in groupBoxs) { if (ret.MainPanel.Controls.Count == 0) { g.Location = new Point(0, 0); } else { Control lastCtrl = ret.MainPanel.Controls[ret.MainPanel.Controls.Count - 1]; g.Location = new Point(0, lastCtrl.Bottom); } g.Width = ret.MainPanel.Width; g.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; ret.MainPanel.Controls.Add(g); } ret.OKClicked += ret_OKClicked; return(ret); }
void ret_OKClicked(object sender, EventArgs e) { Dictionary <String, Object> givenValue = new Dictionary <string, object>(); IniWriter writer = IniWriter.Open(this.TargetFileName); foreach (Control c in DataFieldControl) { String fullName = (String)c.Tag; IniReader.OnSerializeNotificationEventArgs arg = FieldDeserializeMap[fullName]; Object value = null; bool textBased = false; if (c is TextBox) { value = ((TextBox)c).Text; textBased = true; } else if (c is ComboBox) { value = ((ComboBox)c).Text; textBased = true; } if (textBased) { if (arg.Field.FieldType == typeof(int)) { int ival = default(int); int.TryParse((String)value, out ival); value = ival; } else if (arg.Field.FieldType == typeof(double)) { double ival = default(double); double.TryParse((String)value, out ival); value = ival; } else if (arg.Field.FieldType == typeof(bool)) { bool ival = default(bool); bool.TryParse((String)value, out ival); value = ival; } else if (arg.Field.FieldType == typeof(Size)) { var list = IniReader.IntListFromString((String)value); Size sz = new Size(); if (list.Count == 2) { sz.Width = list[0]; sz.Height = list[1]; } value = sz; } else if (arg.Field.FieldType.IsEnum) { try { String sval = (String)value.ToString(); object enumVal = Enum.Parse(arg.Field.FieldType, sval); value = sval; } catch (Exception) { } } else if (arg.Field.FieldType == typeof(Point)) { var list = IniReader.IntListFromString((String)value); Point sz = new Point(); if (list.Count == 2) { sz.X = list[0]; sz.Y = list[1]; } value = sz; } else if (arg.Field.FieldType == typeof(Rectangle)) { var list = IniReader.IntListFromString((String)value); Rectangle sz = new Rectangle(); if (list.Count == 4) { sz.X = list[0]; sz.Y = list[1]; sz.Width = list[2]; sz.Height = list[3]; } value = sz; } else if (arg.Field.FieldType == typeof(int[])) { var list = IniReader.IntListFromString((String)value); value = list; } } else { value = c.BackColor; } givenValue[fullName] = value; } writer.GivenValue = givenValue; writer.Serialize(this.Value); writer.Close(); }
public static void DeserializeFields(IniReader reader, object ret, String prefix = "", EventHandler <OnSerializeNotificationEventArgs> OnSerializingMember = null) { Type t = ret.GetType(); foreach (var field in t.GetFields()) { Object FieldValue = null; OnSerializeNotificationEventArgs OnSerializeArgs = new OnSerializeNotificationEventArgs(); OnSerializeArgs.Reader = reader; OnSerializeArgs.Field = field; OnSerializeArgs.Target = ret; if (field.IsPublic) { var fieldType = field.FieldType; String name = prefix + field.Name; OnSerializeArgs.FullName = name; if (fieldType.IsPrimitive) { if (fieldType == typeof(int)) { object val = reader.GetInt(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType == typeof(bool)) { object val = reader.GetBoolean(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType == typeof(double)) { object val = reader.GetDouble(name); field.SetValue(ret, val); FieldValue = val; } } else if (fieldType == typeof(string)) { object val = reader.GetString(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType == typeof(double[])) { String val = reader.GetString(name); List <double> intList = DoubleListFromString(val); field.SetValue(ret, intList.ToArray()); FieldValue = intList.ToArray(); } else if (fieldType == typeof(int[])) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); field.SetValue(ret, intList.ToArray()); FieldValue = intList.ToArray(); } else if (fieldType == typeof(Color)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int a = 0; int r = 0; int g = 0; int b = 0; if (intList.Count == 4) { a = intList[0]; r = intList[1]; g = intList[2]; b = intList[3]; Color clrVal = Color.FromArgb(a, r, g, b); field.SetValue(ret, clrVal); FieldValue = clrVal; } else if (intList.Count == 3) { r = intList[0]; g = intList[1]; b = intList[2]; Color clrVal = Color.FromArgb(255, r, g, b); field.SetValue(ret, clrVal); FieldValue = clrVal; } } else if (fieldType == typeof(Size)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int w = 0; int h = 0; if (intList.Count == 2) { w = intList[0]; h = intList[1]; field.SetValue(ret, new Size(w, h)); } FieldValue = val; } else if (fieldType == typeof(Rectangle)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int x = 0; int y = 0; int w = 0; int h = 0; if (intList.Count == 4) { x = intList[0]; y = intList[1]; w = intList[2]; h = intList[3]; field.SetValue(ret, new Rectangle(x, y, w, h)); } FieldValue = val; } else if (fieldType == typeof(Point)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int x = 0; int y = 0; if (intList.Count == 2) { x = intList[0]; y = intList[1]; field.SetValue(ret, new Point(x, y)); } FieldValue = val; } else if (fieldType.IsClass) { var constructor = fieldType.GetConstructor(new Type[] { }); object fieldContent = null; fieldContent = field.GetValue(ret); if (fieldContent == null) { if (constructor != null) { fieldContent = constructor.Invoke(new Object[] { }); field.SetValue(ret, fieldContent); } } FieldValue = fieldContent; if (fieldContent != null) { DeserializeFields(reader, fieldContent, field.Name + ".", OnSerializingMember); } } OnSerializeArgs.FieldValue = FieldValue; if (OnSerializingMember != null) { OnSerializingMember(reader, OnSerializeArgs); } } } }
public static void DeserializeFields(IniReader reader, object ret, String prefix = "", EventHandler <OnSerializeNotificationEventArgs> OnSerializingMember = null) { Type t = ret.GetType(); foreach (var field in t.GetFields()) { Object FieldValue = null; OnSerializeNotificationEventArgs OnSerializeArgs = new OnSerializeNotificationEventArgs(); OnSerializeArgs.Reader = reader; OnSerializeArgs.Field = field; OnSerializeArgs.Target = ret; if (field.IsLiteral) { continue; } NonSerializedAttribute[] nonSerialize = (NonSerializedAttribute[])field.GetCustomAttributes(typeof(NonSerializedAttribute), false); if (nonSerialize != null && nonSerialize.Length > 0) { continue; } if (field.IsPublic) { var fieldType = field.FieldType; String name = prefix + field.Name; IniFieldNameAttribute iniFieldName = (IniFieldNameAttribute)field.GetCustomAttribute(typeof(IniFieldNameAttribute), true); if (iniFieldName != null && !String.IsNullOrEmpty(iniFieldName.Name)) { name = iniFieldName.Name; } OnSerializeArgs.FullName = name; if (fieldType.IsPrimitive) { if (fieldType == typeof(int)) { int val = reader.GetInt(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType == typeof(bool)) { bool val = reader.GetBoolean(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType == typeof(double)) { double val = reader.GetDouble(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType.IsEnum) { try { String sval = reader.GetString(name); object enumVal = Enum.Parse(fieldType, sval); field.SetValue(ret, enumVal); } catch (Exception) { } } } else if (fieldType == typeof(string)) { object val = reader.GetString(name); field.SetValue(ret, val); FieldValue = val; } else if (fieldType == typeof(double[])) { String val = reader.GetString(name); List <double> intList = DoubleListFromString(val); field.SetValue(ret, intList.ToArray()); FieldValue = intList.ToArray(); } else if (fieldType == typeof(int[])) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); field.SetValue(ret, intList.ToArray()); FieldValue = intList.ToArray(); } else if (fieldType == typeof(Color)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int a = 0; int r = 0; int g = 0; int b = 0; if (intList.Count == 4) { a = intList[0]; r = intList[1]; g = intList[2]; b = intList[3]; Color clrVal = Color.FromArgb(a, r, g, b); field.SetValue(ret, clrVal); FieldValue = clrVal; } else if (intList.Count == 3) { r = intList[0]; g = intList[1]; b = intList[2]; Color clrVal = Color.FromArgb(255, r, g, b); field.SetValue(ret, clrVal); FieldValue = clrVal; } } else if (fieldType == typeof(Size)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int w = 0; int h = 0; if (intList.Count == 2) { w = intList[0]; h = intList[1]; field.SetValue(ret, new Size(w, h)); } FieldValue = val; } else if (fieldType.IsArray && fieldType.GetElementType().IsClass) { // for case of // SomeThing.Count=2 // SomeThing FlattenArrayLengthName arrayLengthName = (FlattenArrayLengthName)field.GetCustomAttribute(typeof(FlattenArrayLengthName), true); if (arrayLengthName != null && !String.IsNullOrEmpty(arrayLengthName.Name)) { Type elementType = fieldType.GetElementType(); Array arrayInstance = Array.CreateInstance(elementType, reader.GetInt(arrayLengthName.Name)); FlattenArrayName arrayName = (FlattenArrayName)field.GetCustomAttribute(typeof(FlattenArrayName), true); for (int i = 0; i < arrayInstance.Length; ++i) { String flattenArrayName = name + "[" + i.ToString() + "]."; if (arrayName != null && !String.IsNullOrEmpty(arrayName.Name) && !String.IsNullOrEmpty(arrayName.Replacement)) { flattenArrayName = arrayName.Name.Replace(arrayName.Replacement, i.ToString()) + "."; } var constructor = elementType.GetConstructor(new Type[] { }); object fieldContent = null; fieldContent = field.GetValue(ret); if (constructor != null) { fieldContent = constructor.Invoke(new Object[] { }); arrayInstance.SetValue(fieldContent, i); } DeserializeFields(reader, arrayInstance.GetValue(i), flattenArrayName, OnSerializingMember); } field.SetValue(ret, arrayInstance); } } else if (fieldType == typeof(Rectangle)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int x = 0; int y = 0; int w = 0; int h = 0; if (intList.Count == 4) { x = intList[0]; y = intList[1]; w = intList[2]; h = intList[3]; field.SetValue(ret, new Rectangle(x, y, w, h)); } FieldValue = val; } else if (fieldType == typeof(Point)) { String val = reader.GetString(name); List <int> intList = IntListFromString(val); int x = 0; int y = 0; if (intList.Count == 2) { x = intList[0]; y = intList[1]; field.SetValue(ret, new Point(x, y)); } FieldValue = val; } else if (fieldType.IsEnum) { try { String sval = reader.GetString(name); object enumVal = Enum.Parse(fieldType, sval); field.SetValue(ret, enumVal); FieldValue = sval; } catch (Exception) { } } else if (fieldType.IsClass) { var constructor = fieldType.GetConstructor(new Type[] { }); object fieldContent = null; fieldContent = field.GetValue(ret); if (fieldContent == null) { if (constructor != null) { fieldContent = constructor.Invoke(new Object[] { }); field.SetValue(ret, fieldContent); } } FieldValue = fieldContent; if (fieldContent != null) { DeserializeFields(reader, fieldContent, field.Name + ".", OnSerializingMember); } } OnSerializeArgs.FieldValue = FieldValue; if (reader.FieldSectionMapper.ContainsKey(name)) { OnSerializeArgs.Section = reader.FieldSectionMapper[name]; } if (OnSerializingMember != null) { OnSerializingMember(reader, OnSerializeArgs); } } } }
public Form BuildForm(String title = "") { UI.SaveConfigurationTemplateForm ret = new UI.SaveConfigurationTemplateForm(); DataFieldControl.Clear(); this.Value = IniReader.Deserialize <T>(this.TargetFileName, HandleDeserializeField); List <GroupBox> groupBoxs = new List <GroupBox>(); if (String.IsNullOrEmpty(title)) { title = this.TargetFileName; } ret.Text = title; foreach (String g in FieldCategoryMap.Keys) { GroupBox gbox = new GroupBox(); groupBoxs.Add(gbox); gbox.Margin = new Padding(3); List <String> names = FieldCategoryMap[g]; TableLayoutPanel tablelayout = new TableLayoutPanel(); tablelayout.RowCount = names.Count; tablelayout.ColumnCount = 2; tablelayout.AutoScroll = true; for (int i = 0; i < 2; ++i) { ColumnStyle rs = new ColumnStyle(); tablelayout.ColumnStyles.Add(rs); tablelayout.ColumnStyles[i].SizeType = SizeType.Percent; tablelayout.ColumnStyles[i].Width = 0.5f; } if (String.IsNullOrEmpty(g)) { gbox.Text = "Default"; } else { gbox.Text = g; } int row = 0; foreach (String name in names) { IniReader.OnSerializeNotificationEventArgs arg = FieldDeserializeMap[name]; if (arg.Field.FieldType.IsClass) { if (arg.Field.FieldType != typeof(String) && arg.Field.FieldType != typeof(Rectangle) && arg.Field.FieldType != typeof(Size) && arg.Field.FieldType != typeof(Point) && arg.Field.FieldType != typeof(Color) && arg.Field.FieldType != typeof(int[])) { continue; } } RowStyle rs = new RowStyle(); tablelayout.RowStyles.Add(rs); rs.SizeType = SizeType.Absolute; rs.Height = 30; Panel left = new Panel(); Panel right = new Panel(); left.Dock = DockStyle.Fill; right.Dock = DockStyle.Fill; left.Margin = new Padding(0); right.Margin = new Padding(0); Label nameLabel = new Label(); String strname = GetPropertyDisplayName(arg.Field); if (String.IsNullOrEmpty(strname)) { strname = arg.FullName; } nameLabel.Text = strname; nameLabel.Dock = DockStyle.Fill; nameLabel.TextAlign = System.Drawing.ContentAlignment.TopRight; left.Controls.Add(nameLabel); IniReader reader = arg.Reader; Type fieldType = arg.Field.FieldType; if (fieldType == typeof(Color)) { Button btn = new Button(); btn.Size = new Size(100, (int)(rs.Height)); btn.BackColor = (Color)arg.FieldValue; btn.Dock = DockStyle.Fill; btn.Click += btn_colorPickerClick; right.Controls.Add(btn); btn.Tag = arg.FullName; this.DataFieldControl.Add(btn); } else { TextBoxEx tbox = new TextBoxEx(); tbox.Size = new Size(100, (int)(rs.Height)); tbox.Dock = DockStyle.Fill; right.Controls.Add(tbox); tbox.Tag = arg.FullName; tbox.Text = (String)arg.FieldValue.ToString(); this.DataFieldControl.Add(tbox); } tablelayout.Height += ((int)(rs.Height)); tablelayout.Controls.Add(left); tablelayout.Controls.Add(right); tablelayout.SetCellPosition(left, new TableLayoutPanelCellPosition(0, row)); tablelayout.SetCellPosition(right, new TableLayoutPanelCellPosition(1, row)); ++row; } tablelayout.Dock = DockStyle.Fill; gbox.Height = tablelayout.Height; gbox.Controls.Add(tablelayout); } foreach (var g in groupBoxs) { if (ret.MainPanel.Controls.Count == 0) { g.Location = new Point(0, 0); } else { Control lastCtrl = ret.MainPanel.Controls[ret.MainPanel.Controls.Count - 1]; g.Location = new Point(0, lastCtrl.Bottom); } g.Width = ret.MainPanel.Width; g.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; ret.MainPanel.Controls.Add(g); } ret.OKClicked += ret_OKClicked; return(ret); }