Фиксы в первой и вторых лабах

This commit is contained in:
Илья 2024-09-23 13:56:22 +04:00
parent 9725f07d76
commit 5177f3c8e1
5 changed files with 89 additions and 32 deletions

View File

@ -158,7 +158,7 @@ namespace RodionovLibrary.NonVisualComponents
TableRow headerRow = new(); TableRow headerRow = new();
foreach (var column in columnParameters) foreach (var column in columnParameters)
{ {
TableCell cell = new(new Paragraph(new Run(new Text(column.Header == "MergeCell" ? "" : column.Header)))); TableCell cell = new(new Paragraph(new Run(new Text(column.Header))));
TableCellProperties cellProps = new() TableCellProperties cellProps = new()
{ {
TableCellWidth = new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = (column.Width * 566.93).ToString("F0") }, TableCellWidth = new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = (column.Width * 566.93).ToString("F0") },

View File

@ -14,7 +14,7 @@
{ {
get get
{ {
return comboBox.SelectedItem != null ? comboBox.SelectedItem.ToString()! : ""; return comboBox.SelectedItem?.ToString() ?? "";
} }
set set
{ {

View File

@ -1,4 +1,6 @@
using System.Reflection; using DocumentFormat.OpenXml.ExtendedProperties;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Reflection;
namespace RodionovLibrary.VisualComponents namespace RodionovLibrary.VisualComponents
{ {
@ -40,33 +42,49 @@ namespace RodionovLibrary.VisualComponents
throw new ArgumentException("Не хватает данных"); throw new ArgumentException("Не хватает данных");
var type = typeof(T); var type = typeof(T);
var fields = type.GetFields();
var properties = type.GetProperties(); var properties = type.GetProperties();
var curObject = Activator.CreateInstance(type); var members = fields.Cast<MemberInfo>().Concat(properties.Cast<MemberInfo>()).ToArray();
string[] wordsTemplate = _template.Split(' '); var curObject = Activator.CreateInstance(type) ?? throw new Exception("Не получилось создать объект заданного типа");
string[] words = listBox.SelectedItem.ToString().Split(' '); string text = listBox.SelectedItem?.ToString() ?? "";
for (int i = 0; i < wordsTemplate.Length; i++) var fixedParts = System.Text.RegularExpressions.Regex.Split(_template, $@"\{_fromChar.Value}.*?\{_toChar.Value}");
int firstFixedStart = text.IndexOf(fixedParts[0], 0);
if (firstFixedStart == -1)
throw new Exception("Не найден элемент шаблона");
if (firstFixedStart != 0)
{ {
string word = wordsTemplate[i]; string beginning = text[..firstFixedStart];
if (word.Contains(_fromChar.Value) && word.Contains(_toChar.Value)) FillMember(_template.Substring(1, firstFixedStart - 2), curObject, beginning, members);
{
int startCharIndex = word.IndexOf(_fromChar.Value);
int endCharIndex = word.LastIndexOf(_toChar.Value);
string propertyName = word.Substring(startCharIndex + 1, endCharIndex - startCharIndex - 1);
var property = properties.FirstOrDefault(x => x.Name == propertyName);
if (property == null)
continue;
int extraCharsBefore = startCharIndex;
int extraCharsAfter = word.Length - endCharIndex - 1;
string propertyValue = words[i].Substring(extraCharsBefore,
words[i].Length - extraCharsBefore - extraCharsAfter);
property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType));
}
} }
int start = 0;
for (int i = 0; i < fixedParts.Length - 1; i++)
{
start = text.IndexOf(fixedParts[i], start);
if (start == -1)
throw new Exception("Не найден элемент шаблона");
start += fixedParts[i].Length;
int nextFixedIndex = text.IndexOf(fixedParts[i + 1], start);
if (nextFixedIndex == -1)
throw new Exception("Не найден следующий элемент шаблона");
string valueBetween = text[start..nextFixedIndex];
string templatePart = _template.Substring(_template.IndexOf(fixedParts[i]) + fixedParts[i].Length);
int startCharIndex = templatePart.IndexOf(_fromChar.Value);
int endCharIndex = templatePart.IndexOf(_toChar.Value);
string memberName = templatePart.Substring(startCharIndex + 1, endCharIndex - startCharIndex - 1);
FillMember(memberName, curObject, valueBetween, members);
start = nextFixedIndex;
}
return (T?)curObject; return (T?)curObject;
} }
@ -77,17 +95,57 @@ namespace RodionovLibrary.VisualComponents
throw new ArgumentException("Не хватает данных"); throw new ArgumentException("Не хватает данных");
listBox.Items.Clear(); listBox.Items.Clear();
var type = typeof(T); var type = typeof(T);
var fields = type.GetFields();
var properties = type.GetProperties(); var properties = type.GetProperties();
var members = fields.Cast<MemberInfo>().Concat(properties.Cast<MemberInfo>()).ToArray();
foreach (T item in items) foreach (T item in items)
{ {
string result = _template; string result = _template;
foreach (var property in properties) foreach (var member in members)
{ {
string search = _fromChar.Value + property.Name + _toChar.Value; string search = _fromChar.Value + member.Name + _toChar.Value;
result = result.Replace(search, property.GetValue(item)?.ToString() ?? ""); object? value = null;
if (member is FieldInfo field)
{
value = field.GetValue(item);
}
if (member is PropertyInfo property)
{
if (property.CanRead)
{
value = property.GetValue(item);
}
}
result = result.Replace(search, value?.ToString() ?? "");
} }
listBox.Items.Add(result); listBox.Items.Add(result);
} }
} }
private void FillMember(string memberName, object curObject, string value, MemberInfo[]? members)
{
var member = members?.FirstOrDefault(x => x.Name == memberName)
?? throw new Exception("Ошибка с поиском элемента");
object convertedValue = Convert.ChangeType(value, GetMemberType(member));
SetMemberValue(curObject, member, convertedValue);
}
private Type GetMemberType(MemberInfo member)
{
return member is PropertyInfo property ? property.PropertyType : ((FieldInfo)member).FieldType;
}
private void SetMemberValue(object obj, MemberInfo member, object value)
{
if (member is PropertyInfo property)
{
property.SetValue(obj, value);
}
else if (member is FieldInfo field)
{
field.SetValue(obj, value);
}
}
} }
} }

View File

@ -47,7 +47,6 @@ namespace RodionovLibrary.VisualComponents
private void TextBox_TextChanged(object sender, EventArgs e) private void TextBox_TextChanged(object sender, EventArgs e)
{ {
_valueChanged?.Invoke(sender, e); _valueChanged?.Invoke(sender, e);
Value = textBox.Text;
} }
private void TextBox_MouseEnter(object sender, EventArgs e) private void TextBox_MouseEnter(object sender, EventArgs e)

View File

@ -99,12 +99,12 @@ namespace WinForms
new() { Header = "Čäĺíň", PropertyName = "", Width = 1.2 }, new() { Header = "Čäĺíň", PropertyName = "", Width = 1.2 },
new() { Header = "Ńňŕňóń", PropertyName = "", Width = 1.2 }, new() { Header = "Ńňŕňóń", PropertyName = "", Width = 1.2 },
new() { Header = "Ëč÷íűĺ äŕííűĺ", PropertyName = "", Width = 1.6 }, new() { Header = "Ëč÷íűĺ äŕííűĺ", PropertyName = "", Width = 1.6 },
new() { Header = "MergeCell", PropertyName = "", Width = 1.6 }, new() { Header = "Ëč÷íűĺ äŕííűĺ", PropertyName = "", Width = 1.6 },
new() { Header = "MergeCell", PropertyName = "", Width = 1.6 }, new() { Header = "Ëč÷íűĺ äŕííűĺ", PropertyName = "", Width = 1.6 },
new() { Header = "Äĺňč", PropertyName = "", Width = 1.2 }, new() { Header = "Äĺňč", PropertyName = "", Width = 1.2 },
new() { Header = "Ěŕřčíŕ", PropertyName = "", Width = 1.6 }, new() { Header = "Ěŕřčíŕ", PropertyName = "", Width = 1.6 },
new() { Header = "Đŕáîňŕ", PropertyName = "", Width = 2.5 }, new() { Header = "Đŕáîňŕ", PropertyName = "", Width = 2.5 },
new() { Header = "MergeCell", PropertyName = "", Width = 2.5 }, new() { Header = "Đŕáîňŕ", PropertyName = "", Width = 2.5 },
new() { Header = "Ďđĺěč˙", PropertyName = "", Width = 2 } new() { Header = "Ďđĺěč˙", PropertyName = "", Width = 2 }
}; };