This commit is contained in:
Александр Бабаев 2024-03-26 18:50:21 +03:00
parent 16cd01cc7e
commit 8f6439b7d8
11 changed files with 601 additions and 70 deletions

View File

@ -1,5 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
using anbs_cp.Enums;
using anbs_cp.Interfaces;
@ -23,10 +24,7 @@ namespace anbs_cp.Classes;
* Заменены интерфейсы IAction* на соответствующие классы
*/
/// <summary>
/// Класс, хранящий в себе состояния действия
/// </summary>
/// <typeparam name="T">Тип значения - результата действия</typeparam>
/// <inheritdoc />
public class ActionState<T>: IActionState
{
/// <summary>
@ -350,4 +348,91 @@ public class ActionState<T>: IActionState
}
#endregion
#endregion
#region Реализация интерфейса ISerializable
/// <inheritdoc />
public string Serialize ()
{
// Создаю модель
ActionStateSerializable serializableModel = new();
// Для каждого информационного сообщения
foreach (IActionStateMessage info in Info)
// - добавляю в модель
serializableModel.Info.Add(info.Serialize());
// Для каждого предупреждения
foreach (IActionStateMessage warning in Warnings)
// - добавляю в модель
serializableModel.Info.Add(warning.Serialize());
// Для каждой ошибки
foreach (IActionStateMessage error in Errors)
// - добавляю в модель
serializableModel.Info.Add(error.Serialize());
// Создаю модель значения
ActionStateSerializableValue value = new();
// Получаю данные
value.GetValue(Value);
// Добавляю в модель
serializableModel.Value = value.Serialize();
// Возвращаю сериализованную модель
return new SysTextSerializer().Serialize(serializableModel);
}
/// <inheritdoc />
public void Deserialize (string json)
{
// Десериализую строку
ActionStateSerializable itemSerializable = new SysTextSerializer().Deserialize<ActionStateSerializable>(json) ?? new();
// Создаю модель значения
ActionStateSerializableValue value = new();
// Очищаю списки
Info.Clear();
Warnings.Clear();
Errors.Clear();
// Для каждого информационного сообщения
foreach (string infoString in itemSerializable.Info)
{
// - создаю сообщение
ActionStateMessage info = new();
// - десериализую в него данные из модели
info.Deserialize(infoString);
// - добавляю в список
Info.Add(info);
}
foreach (string warningString in itemSerializable.Warnings)
{
// - создаю сообщение
ActionStateMessage warning = new();
// - десериализую в него данные из модели
warning.Deserialize(warningString);
// - добавляю в список
Info.Add(warning);
}
foreach (string errorString in itemSerializable.Errors)
{
// - создаю сообщение
ActionStateMessage error = new();
// - десериализую в него данные из модели
error.Deserialize(errorString);
// - добавляю в список
Info.Add(error);
}
// Десериализую данные значения из модели
value.Deserialize(itemSerializable.Value);
// Получаю значение
Value = value.SetValue<T>();
}
#endregion
}

View File

@ -0,0 +1,27 @@
namespace anbs_cp.Classes;
/// <summary>
/// Сериализованная модель класса, хранящего в себе состояния действия
/// </summary>
public class ActionStateSerializable
{
/// <summary>
/// Список информации
/// </summary>
public List<string> Info { get; } = [];
/// <summary>
/// Список предупреждений
/// </summary>
public List<string> Warnings { get; } = [];
/// <summary>
/// Список ошибок
/// </summary>
public List<string> Errors { get; } = [];
/// <summary>
/// Значение
/// </summary>
public string Value { get; set; } = string.Empty;
}

View File

@ -0,0 +1,90 @@
using anbs_cp.Interfaces;
namespace anbs_cp.Classes;
/// <summary>
/// Сериализованная модель значения класса, хранящего в себе состояния действия
/// </summary>
public class ActionStateSerializableValue: ISerializable
{
/// <summary>
/// Значение - null?
/// </summary>
private bool IsNull { get; set; }
/// <summary>
/// Сериализуемое значение
/// </summary>
private string Serialized { get; set; } = string.Empty;
/// <summary>
/// Получает значение из <paramref name="value"/>
/// </summary>
/// <param name="value">Значение</param>
/// <typeparam name="T">Тип значения</typeparam>
public void GetValue<T> (T? value)
{
// Значение - null?
IsNull = value == null;
// Если не null
if (value != null)
{
// - значение поддерживает интерфейс IsSerialized
if (value is ISerializable serializableValue)
// -- сериализую
Serialized = serializableValue.Serialize();
else
// -- сериализую
Serialized = new SysTextSerializer().Serialize(value);
}
else
// Сериализую
Serialized = new SysTextSerializer().Serialize(value);
}
/// <summary>
/// Устанавливает значение
/// </summary>
/// <typeparam name="T">Класс</typeparam>
/// <returns>Значение</returns>
public T? SetValue<T> ()
{
// Если null
if (IsNull)
// - то возыращаем null
return default;
// Если тип T не реализует интерфейс ISerializable
if (typeof(T).GetInterface(nameof(ISerializable)) == null)
// - то воспользуемся простой десериализацией
return new SysTextSerializer().Deserialize<T>(Serialized);
// Создаю модель
T? model = default;
// Десериализую модель
(model as ISerializable)?.Deserialize(Serialized);
// Возвращаю модель
return model!;
// Если null
}
#region Реализация интерфейса ISerializable
/// <inheritdoc />
public string Serialize () => new SysTextSerializer().Serialize(this);
/// <inheritdoc />
public void Deserialize (string json)
{
// Десериализую строку
ActionStateSerializableValue item = new SysTextSerializer().Deserialize<ActionStateSerializableValue>(json) ?? new();
// Передаю параметры
IsNull = item.IsNull;
Serialized = item.Serialized;
}
#endregion
}

View File

@ -3,9 +3,9 @@
namespace anbs_cp.Interfaces;
/// <summary>
/// Интерфейс класса, хранящего в себе состояния действия
/// Класс, хранящий в себе состояния действия
/// </summary>
public interface IActionState
public interface IActionState: ISerializable
{
/// <summary>
/// Список информации

92
demo/ActionStateSerialize.Designer.cs generated Normal file
View File

@ -0,0 +1,92 @@
namespace demo;
partial class ActionStateSerialize
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose (bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent ()
{
textBox1 = new TextBox();
textBox2 = new TextBox();
label1 = new Label();
button1 = new Button();
SuspendLayout();
//
// textBox1
//
textBox1.Location = new Point(24, 12);
textBox1.Name = "textBox1";
textBox1.Size = new Size(209, 23);
textBox1.TabIndex = 0;
//
// textBox2
//
textBox2.Location = new Point(312, 6);
textBox2.Multiline = true;
textBox2.Name = "textBox2";
textBox2.Size = new Size(463, 225);
textBox2.TabIndex = 1;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(24, 65);
label1.Name = "label1";
label1.Size = new Size(38, 15);
label1.TabIndex = 2;
label1.Text = "label1";
//
// button1
//
button1.Location = new Point(24, 111);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 3;
button1.Text = "button1";
button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
//
// ActionStateSerialize
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 250);
Controls.Add(button1);
Controls.Add(label1);
Controls.Add(textBox2);
Controls.Add(textBox1);
Name = "ActionStateSerialize";
Text = "Тест сериализации ActionState";
ResumeLayout(false);
PerformLayout();
}
#endregion
private TextBox textBox1;
private TextBox textBox2;
private Label label1;
private Button button1;
}

View File

@ -0,0 +1,31 @@
using anbs_cp.Classes;
namespace demo;
public partial class ActionStateSerialize: Form
{
public ActionStateSerialize ()
{
InitializeComponent();
}
private void button1_Click (object sender, EventArgs e)
{
DemoClass demoClass = new()
{
Name = textBox1.Text.Length > 0 ? textBox1.Text : string.Empty
};
ActionState<DemoClass> state = new();
state.AddError("ОШИБКА, ШЕФ!");
textBox2.Text = state.Serialize();
ActionState<DemoClass> state2 = new();
state2.Deserialize(textBox2.Text);
label1.Text = state2.Value?.Name ?? "НЕТ ЗНАЧЕНИЯ";
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

8
demo/DemoClass.cs Normal file
View File

@ -0,0 +1,8 @@
namespace demo;
public class DemoClass
{
public int Num { get; set; } = 5;
public string Name { get; set; } = "";
public bool NotB { get; set; } = true;
}

View File

@ -28,69 +28,80 @@ sealed partial class MainMenu
/// </summary>
private void InitializeComponent ()
{
this.CountValueTest = new System.Windows.Forms.Button();
this.SimpleMapperTest = new System.Windows.Forms.Button();
this.FileExtensionTest = new System.Windows.Forms.Button();
this.OsInfoBtn = 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 = "Новый тест модуля форматирования строки";
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 = "Новый тест модуля SimpleMapper";
this.SimpleMapperTest.UseVisualStyleBackColor = true;
this.SimpleMapperTest.Click += new System.EventHandler(this.SimpleMapperTest_Click);
//
// FileExtensionTest
//
this.FileExtensionTest.Location = new System.Drawing.Point(12, 128);
this.FileExtensionTest.Name = "FileExtensionTest";
this.FileExtensionTest.Size = new System.Drawing.Size(335, 51);
this.FileExtensionTest.TabIndex = 2;
this.FileExtensionTest.Text = "Новый тест модуля FileExtension";
this.FileExtensionTest.UseVisualStyleBackColor = true;
this.FileExtensionTest.Click += new System.EventHandler(this.FileExtensionTest_Click);
//
// OsInfoBtn
//
this.OsInfoBtn.Location = new System.Drawing.Point(12, 185);
this.OsInfoBtn.Name = "OsInfoBtn";
this.OsInfoBtn.Size = new System.Drawing.Size(335, 51);
this.OsInfoBtn.TabIndex = 3;
this.OsInfoBtn.Text = "Информация о системе";
this.OsInfoBtn.UseVisualStyleBackColor = true;
this.OsInfoBtn.Click += new System.EventHandler(this.OsInfoBtn_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.OsInfoBtn);
this.Controls.Add(this.FileExtensionTest);
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.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "MainMenu";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Главное меню";
this.ResumeLayout(false);
CountValueTest = new Button();
SimpleMapperTest = new Button();
FileExtensionTest = new Button();
OsInfoBtn = new Button();
actionStateSerializerTestBtn = new Button();
SuspendLayout();
//
// CountValueTest
//
CountValueTest.Location = new Point(12, 12);
CountValueTest.Name = "CountValueTest";
CountValueTest.Size = new Size(337, 53);
CountValueTest.TabIndex = 0;
CountValueTest.Text = "Новый тест модуля форматирования строки";
CountValueTest.UseVisualStyleBackColor = true;
CountValueTest.Click += button1_Click;
//
// SimpleMapperTest
//
SimpleMapperTest.Location = new Point(12, 71);
SimpleMapperTest.Name = "SimpleMapperTest";
SimpleMapperTest.Size = new Size(335, 51);
SimpleMapperTest.TabIndex = 1;
SimpleMapperTest.Text = "Новый тест модуля SimpleMapper";
SimpleMapperTest.UseVisualStyleBackColor = true;
SimpleMapperTest.Click += SimpleMapperTest_Click;
//
// FileExtensionTest
//
FileExtensionTest.Location = new Point(12, 128);
FileExtensionTest.Name = "FileExtensionTest";
FileExtensionTest.Size = new Size(335, 51);
FileExtensionTest.TabIndex = 2;
FileExtensionTest.Text = "Новый тест модуля FileExtension";
FileExtensionTest.UseVisualStyleBackColor = true;
FileExtensionTest.Click += FileExtensionTest_Click;
//
// OsInfoBtn
//
OsInfoBtn.Location = new Point(12, 185);
OsInfoBtn.Name = "OsInfoBtn";
OsInfoBtn.Size = new Size(335, 51);
OsInfoBtn.TabIndex = 3;
OsInfoBtn.Text = "Информация о системе";
OsInfoBtn.UseVisualStyleBackColor = true;
OsInfoBtn.Click += OsInfoBtn_Click;
//
// actionStateSerializerTestBtn
//
actionStateSerializerTestBtn.Location = new Point(12, 254);
actionStateSerializerTestBtn.Name = "actionStateSerializerTestBtn";
actionStateSerializerTestBtn.Size = new Size(335, 51);
actionStateSerializerTestBtn.TabIndex = 4;
actionStateSerializerTestBtn.Text = "Тест сериализации ActionState";
actionStateSerializerTestBtn.UseVisualStyleBackColor = true;
actionStateSerializerTestBtn.Click += actionStateSerializerTestBtn_Click;
//
// MainMenu
//
AutoScaleDimensions = new SizeF(10F, 21F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(361, 327);
Controls.Add(actionStateSerializerTestBtn);
Controls.Add(OsInfoBtn);
Controls.Add(FileExtensionTest);
Controls.Add(SimpleMapperTest);
Controls.Add(CountValueTest);
Font = new Font("Times New Roman", 14.25F);
FormBorderStyle = FormBorderStyle.FixedSingle;
Margin = new Padding(4);
Name = "MainMenu";
StartPosition = FormStartPosition.CenterScreen;
Text = "Главное меню";
ResumeLayout(false);
}
#endregion
@ -99,4 +110,5 @@ sealed partial class MainMenu
private Button SimpleMapperTest;
private Button FileExtensionTest;
private Button OsInfoBtn;
private Button actionStateSerializerTestBtn;
}

View File

@ -29,4 +29,10 @@ public sealed partial class MainMenu: Form
OsInfoFrm formTest = new();
formTest.ShowDialog();
}
private void actionStateSerializerTestBtn_Click (object sender, EventArgs e)
{
ActionStateSerialize form = new();
form.ShowDialog();
}
}

View File

@ -1,4 +1,64 @@
<root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">