From 87128879a575a8b00e6551bbc7b3fad7dbf9650f Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 25 Feb 2022 07:10:09 +0300 Subject: [PATCH] 20220225 --- anbs_cp/SimpleMapper.cs | 12 +- anbs_cp/anbs_cp.csproj | 6 +- demo/MainMenu.Designer.cs | 75 ++++++++++++ demo/MainMenu.cs | 30 +++++ demo/MainMenu.resx | 60 ++++++++++ demo/Program.cs | 2 +- demo/SampleMapperTest.Designer.cs | 189 ++++++++++++++++++++++++++++++ demo/SampleMapperTest.cs | 73 ++++++++++++ demo/SampleMapperTest.resx | 65 ++++++++++ demo/demo.csproj | 16 ++- demo/demo.csproj.DotSettings | 2 + 11 files changed, 519 insertions(+), 11 deletions(-) create mode 100644 demo/MainMenu.Designer.cs create mode 100644 demo/MainMenu.cs create mode 100644 demo/MainMenu.resx create mode 100644 demo/SampleMapperTest.Designer.cs create mode 100644 demo/SampleMapperTest.cs create mode 100644 demo/SampleMapperTest.resx create mode 100644 demo/demo.csproj.DotSettings diff --git a/anbs_cp/SimpleMapper.cs b/anbs_cp/SimpleMapper.cs index cf949ba..f6a19d5 100644 --- a/anbs_cp/SimpleMapper.cs +++ b/anbs_cp/SimpleMapper.cs @@ -18,7 +18,7 @@ public static class SimpleMapper /// Тип сопоставления /// Список параметров для сопоставления /// Класс-родитель - /// Класс-приёмник + /// Класс-приемник public static void MapEx (TF from, ref T to, MapMode mode, List list) { //Копирую поля @@ -27,7 +27,7 @@ public static class SimpleMapper foreach (FieldInfo fieldOfA in typeOfA.GetFields()) { //Проверяем выполнение условия и прерываем, если не выполняется - if (CheckCondition(fieldOfA.Name, fieldOfA.GetValue(from), mode, list)) + if (!CheckCondition(fieldOfA.Name, fieldOfA.GetValue(from), mode, list)) continue; //Получаем FieldInfo для b по имени поля a @@ -41,7 +41,7 @@ public static class SimpleMapper foreach (PropertyInfo propertyOfA in typeOfA.GetProperties()) { //Проверяем выполнение условия и прерываем, если не выполняется - if (CheckCondition(propertyOfA.Name, propertyOfA.GetValue(from), mode, list)) + if (!CheckCondition(propertyOfA.Name, propertyOfA.GetValue(from), mode, list)) continue; //Получаем PropertyInfo для b по имени свойства a @@ -56,7 +56,7 @@ public static class SimpleMapper /// /// Параметр класса F /// Класс-родитель - /// Класс-приёмник + /// Класс-приемник /// Элемент класса T public static T Map (TF from) { @@ -77,7 +77,7 @@ public static class SimpleMapper /// Режим проверки /// Список игнорирования/добавления /// - private static bool CheckCondition (string itemName, object? itemValue, MapMode mode, ICollection list) + private static bool CheckCondition (string itemName, object? itemValue, MapMode mode, ICollection list) { //Если режим "Только список" и поля нет в списке, //либо режим "Только не в списке" и поле есть в списке @@ -91,7 +91,7 @@ public static class SimpleMapper MapMode.MapByList => list.Contains(itemName), MapMode.MapIgnoreList => !list.Contains(itemName), MapMode.MapNotDefault => itemValue != default, - MapMode.MapNotNullOrDefault => itemValue != null || itemValue != default, + MapMode.MapNotNullOrDefault => !Equals(itemValue, default(T)), _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null) }; diff --git a/anbs_cp/anbs_cp.csproj b/anbs_cp/anbs_cp.csproj index 0e403bf..5c3a4ed 100644 --- a/anbs_cp/anbs_cp.csproj +++ b/anbs_cp/anbs_cp.csproj @@ -2,7 +2,7 @@ net6.0 - 1.2022.223 + 1.2022.225 Alexander Babaev ANB Software Components Pack Library of some useful functions in C# language. @@ -15,8 +15,8 @@ False https://github.com/GoodBoyAlex/anbsoftware_componentspack https://github.com/GoodBoyAlex/anbsoftware_componentspack - 1.2022.0223 - 1.2022.223 + 1.2022.0225 + 1.2022.225 ANBSoftware.ComponentsPack MIT 6.0 diff --git a/demo/MainMenu.Designer.cs b/demo/MainMenu.Designer.cs new file mode 100644 index 0000000..9f45d05 --- /dev/null +++ b/demo/MainMenu.Designer.cs @@ -0,0 +1,75 @@ +namespace demo; + +sealed partial class MainMenu +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose (bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent () + { + this.CountValueTest = new System.Windows.Forms.Button(); + this.SimpleMapperTest = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // CountValueTest + // + this.CountValueTest.Location = new System.Drawing.Point(12, 12); + this.CountValueTest.Name = "CountValueTest"; + this.CountValueTest.Size = new System.Drawing.Size(337, 53); + this.CountValueTest.TabIndex = 0; + this.CountValueTest.Text = "New CountValue Test"; + this.CountValueTest.UseVisualStyleBackColor = true; + this.CountValueTest.Click += new System.EventHandler(this.button1_Click); + // + // SimpleMapperTest + // + this.SimpleMapperTest.Location = new System.Drawing.Point(12, 71); + this.SimpleMapperTest.Name = "SimpleMapperTest"; + this.SimpleMapperTest.Size = new System.Drawing.Size(335, 51); + this.SimpleMapperTest.TabIndex = 1; + this.SimpleMapperTest.Text = "New SimpleMapper test"; + this.SimpleMapperTest.UseVisualStyleBackColor = true; + this.SimpleMapperTest.Click += new System.EventHandler(this.SimpleMapperTest_Click); + // + // MainMenu + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(361, 252); + this.Controls.Add(this.SimpleMapperTest); + this.Controls.Add(this.CountValueTest); + this.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Margin = new System.Windows.Forms.Padding(4); + this.Name = "MainMenu"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Main menu"; + this.ResumeLayout(false); + + } + + #endregion + + private Button CountValueTest; + private Button SimpleMapperTest; +} diff --git a/demo/MainMenu.cs b/demo/MainMenu.cs new file mode 100644 index 0000000..1d5505b --- /dev/null +++ b/demo/MainMenu.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace demo; +public sealed partial class MainMenu: Form +{ + public MainMenu () + { + InitializeComponent(); + } + + private void button1_Click (object sender, EventArgs e) + { + CountValueTest formCountValueTest = new(); + formCountValueTest.ShowDialog(); + } + + private void SimpleMapperTest_Click (object sender, EventArgs e) + { + SampleMapperTest formSampleMapperTest = new(); + formSampleMapperTest.ShowDialog(); + } +} diff --git a/demo/MainMenu.resx b/demo/MainMenu.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/demo/MainMenu.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/demo/Program.cs b/demo/Program.cs index bcf1871..44dabc6 100644 --- a/demo/Program.cs +++ b/demo/Program.cs @@ -9,6 +9,6 @@ internal static class Program static void Main () { ApplicationConfiguration.Initialize(); - Application.Run(new CountValueTest()); + Application.Run(new MainMenu()); } } \ No newline at end of file diff --git a/demo/SampleMapperTest.Designer.cs b/demo/SampleMapperTest.Designer.cs new file mode 100644 index 0000000..f1cc47e --- /dev/null +++ b/demo/SampleMapperTest.Designer.cs @@ -0,0 +1,189 @@ +namespace demo; + +sealed partial class SampleMapperTest +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose (bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent () + { + this.DemoBoolEdt = new System.Windows.Forms.CheckBox(); + this.DemoStringEdt = new System.Windows.Forms.TextBox(); + this.DemoIntEdt = new System.Windows.Forms.NumericUpDown(); + this.DemoDateTimeEdt = new System.Windows.Forms.DateTimePicker(); + this.MapBtn = new System.Windows.Forms.Button(); + this.ResultArea = new System.Windows.Forms.TextBox(); + this.DemoStringLabel = new System.Windows.Forms.Label(); + this.DemoIntLabel = new System.Windows.Forms.Label(); + this.DemoDateTimeLabel = new System.Windows.Forms.Label(); + this.MapModeEdit = new System.Windows.Forms.ComboBox(); + this.MapModeLabel = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.DemoIntEdt)).BeginInit(); + this.SuspendLayout(); + // + // DemoBoolEdt + // + this.DemoBoolEdt.AutoSize = true; + this.DemoBoolEdt.Location = new System.Drawing.Point(32, 144); + this.DemoBoolEdt.Name = "DemoBoolEdt"; + this.DemoBoolEdt.Size = new System.Drawing.Size(65, 25); + this.DemoBoolEdt.TabIndex = 0; + this.DemoBoolEdt.Text = "Bool"; + this.DemoBoolEdt.UseVisualStyleBackColor = true; + // + // DemoStringEdt + // + this.DemoStringEdt.Location = new System.Drawing.Point(32, 42); + this.DemoStringEdt.Name = "DemoStringEdt"; + this.DemoStringEdt.Size = new System.Drawing.Size(737, 29); + this.DemoStringEdt.TabIndex = 1; + // + // DemoIntEdt + // + this.DemoIntEdt.Location = new System.Drawing.Point(32, 109); + this.DemoIntEdt.Name = "DemoIntEdt"; + this.DemoIntEdt.Size = new System.Drawing.Size(737, 29); + this.DemoIntEdt.TabIndex = 2; + // + // DemoDateTimeEdt + // + this.DemoDateTimeEdt.CustomFormat = "dd.MM.yyyy HH:mm:ss"; + this.DemoDateTimeEdt.Format = System.Windows.Forms.DateTimePickerFormat.Custom; + this.DemoDateTimeEdt.Location = new System.Drawing.Point(32, 193); + this.DemoDateTimeEdt.Name = "DemoDateTimeEdt"; + this.DemoDateTimeEdt.Size = new System.Drawing.Size(737, 29); + this.DemoDateTimeEdt.TabIndex = 3; + // + // MapBtn + // + this.MapBtn.Location = new System.Drawing.Point(32, 306); + this.MapBtn.Name = "MapBtn"; + this.MapBtn.Size = new System.Drawing.Size(737, 57); + this.MapBtn.TabIndex = 5; + this.MapBtn.Text = "MAP"; + this.MapBtn.UseVisualStyleBackColor = true; + this.MapBtn.Click += new System.EventHandler(this.MapBtn_Click); + // + // ResultArea + // + this.ResultArea.Dock = System.Windows.Forms.DockStyle.Right; + this.ResultArea.Location = new System.Drawing.Point(811, 0); + this.ResultArea.Multiline = true; + this.ResultArea.Name = "ResultArea"; + this.ResultArea.ReadOnly = true; + this.ResultArea.ScrollBars = System.Windows.Forms.ScrollBars.Both; + this.ResultArea.Size = new System.Drawing.Size(332, 378); + this.ResultArea.TabIndex = 6; + // + // DemoStringLabel + // + this.DemoStringLabel.AutoSize = true; + this.DemoStringLabel.Location = new System.Drawing.Point(32, 18); + this.DemoStringLabel.Name = "DemoStringLabel"; + this.DemoStringLabel.Size = new System.Drawing.Size(54, 21); + this.DemoStringLabel.TabIndex = 7; + this.DemoStringLabel.Text = "String"; + // + // DemoIntLabel + // + this.DemoIntLabel.AutoSize = true; + this.DemoIntLabel.Location = new System.Drawing.Point(32, 85); + this.DemoIntLabel.Name = "DemoIntLabel"; + this.DemoIntLabel.Size = new System.Drawing.Size(30, 21); + this.DemoIntLabel.TabIndex = 8; + this.DemoIntLabel.Text = "Int"; + // + // DemoDateTimeLabel + // + this.DemoDateTimeLabel.AutoSize = true; + this.DemoDateTimeLabel.Location = new System.Drawing.Point(32, 169); + this.DemoDateTimeLabel.Name = "DemoDateTimeLabel"; + this.DemoDateTimeLabel.Size = new System.Drawing.Size(81, 21); + this.DemoDateTimeLabel.TabIndex = 9; + this.DemoDateTimeLabel.Text = "DateTime"; + // + // MapModeEdit + // + this.MapModeEdit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.MapModeEdit.FormattingEnabled = true; + this.MapModeEdit.Items.AddRange(new object[] { + "Full", + "Not null", + "Not default", + "Not null or default"}); + this.MapModeEdit.Location = new System.Drawing.Point(32, 254); + this.MapModeEdit.Name = "MapModeEdit"; + this.MapModeEdit.Size = new System.Drawing.Size(737, 29); + this.MapModeEdit.TabIndex = 10; + // + // MapModeLabel + // + this.MapModeLabel.AutoSize = true; + this.MapModeLabel.Location = new System.Drawing.Point(32, 230); + this.MapModeLabel.Name = "MapModeLabel"; + this.MapModeLabel.Size = new System.Drawing.Size(91, 21); + this.MapModeLabel.TabIndex = 11; + this.MapModeLabel.Text = "Map mode"; + // + // SampleMapperTest + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1143, 378); + this.Controls.Add(this.MapModeLabel); + this.Controls.Add(this.MapModeEdit); + this.Controls.Add(this.DemoDateTimeLabel); + this.Controls.Add(this.DemoIntLabel); + this.Controls.Add(this.DemoStringLabel); + this.Controls.Add(this.ResultArea); + this.Controls.Add(this.MapBtn); + this.Controls.Add(this.DemoDateTimeEdt); + this.Controls.Add(this.DemoIntEdt); + this.Controls.Add(this.DemoStringEdt); + this.Controls.Add(this.DemoBoolEdt); + this.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Margin = new System.Windows.Forms.Padding(4); + this.Name = "SampleMapperTest"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Text = "SampleMapper test"; + ((System.ComponentModel.ISupportInitialize)(this.DemoIntEdt)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private CheckBox DemoBoolEdt; + private TextBox DemoStringEdt; + private NumericUpDown DemoIntEdt; + private DateTimePicker DemoDateTimeEdt; + private Button MapBtn; + private TextBox ResultArea; + private Label DemoStringLabel; + private Label DemoIntLabel; + private Label DemoDateTimeLabel; + private ComboBox MapModeEdit; + private Label MapModeLabel; +} diff --git a/demo/SampleMapperTest.cs b/demo/SampleMapperTest.cs new file mode 100644 index 0000000..361f998 --- /dev/null +++ b/demo/SampleMapperTest.cs @@ -0,0 +1,73 @@ +using anbs_cp; + +using Newtonsoft.Json; + +namespace demo; +public sealed partial class SampleMapperTest: Form +{ + public SampleMapperTest () + { + InitializeComponent(); + } + + private void MapBtn_Click (object sender, EventArgs e) + { + Demo1Class demo1 = new() + { + DemoString = DemoStringEdt.Text, + DemoInt = (int)DemoIntEdt.Value, + DemoBool = DemoBoolEdt.Checked, + DemoDateTime = DemoDateTimeEdt.Value + }; + + Demo1Class demo2 = new() + { + DemoInt = 20220224, + DemoBool = true, + DemoDateTime = default + }; + + string serialize1 = JsonConvert.SerializeObject(demo2); + + SimpleMapper.MapMode mode = MapModeEdit.SelectedIndex switch + { + 0 => SimpleMapper.MapMode.MapFull, + 1 => SimpleMapper.MapMode.MapNotNull, + 2 => SimpleMapper.MapMode.MapNotDefault, + 3 => SimpleMapper.MapMode.MapNotNullOrDefault, + _ => SimpleMapper.MapMode.MapFull + }; + + SimpleMapper.MapEx(demo1, ref demo2, mode, new List()); + + string serialize2 = JsonConvert.SerializeObject(demo2); + + ResultArea.Text = $@"Demo2 Class before map: + {serialize1} + and after:{serialize2}"; + } +} + +public sealed class Demo1Class +{ + public string? DemoString { get; set; } + + public int DemoInt { get; set; } + + public bool DemoBool { get; set; } + + public DateTime DemoDateTime { get; set; } +} + +public class Demo2Class +{ + public string? DemoString { get; set; } + + public int DemoInt { get; set; } + + public bool DemoBool { get; set; } + + public DateTime DemoDateTime { get; set; } + + public string? DemoStringNotMapped { get; set; } +} \ No newline at end of file diff --git a/demo/SampleMapperTest.resx b/demo/SampleMapperTest.resx new file mode 100644 index 0000000..65463ee --- /dev/null +++ b/demo/SampleMapperTest.resx @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Demo2 Class before map: +{0} +and after:{1} + + \ No newline at end of file diff --git a/demo/demo.csproj b/demo/demo.csproj index 10977d6..37355f4 100644 --- a/demo/demo.csproj +++ b/demo/demo.csproj @@ -2,13 +2,27 @@ WinExe - net6.0-windows + net6.0-windows10.0.22000.0 enable true enable Release;Debug.CNF + 7.0 + + + True + + + + True + + + + + + diff --git a/demo/demo.csproj.DotSettings b/demo/demo.csproj.DotSettings new file mode 100644 index 0000000..4887f94 --- /dev/null +++ b/demo/demo.csproj.DotSettings @@ -0,0 +1,2 @@ + + CSharp100 \ No newline at end of file