2024-10-04 11:08:55 +04:00
|
|
|
|
using System.Reflection;
|
2024-09-22 20:37:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace WinFormsLibrary
|
|
|
|
|
{
|
|
|
|
|
public partial class ListBoxValues : UserControl
|
|
|
|
|
{
|
|
|
|
|
private string _layout;
|
2024-10-04 11:08:55 +04:00
|
|
|
|
private char? _endSymbol;
|
|
|
|
|
private char? _startSymbol;
|
2024-09-22 20:37:12 +04:00
|
|
|
|
public ListBoxValues()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2024-10-04 11:08:55 +04:00
|
|
|
|
public void SetLayout(string layout, char? startSymbol, char? endSymbol)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
if (layout == null)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_layout = layout;
|
|
|
|
|
_endSymbol = endSymbol;
|
|
|
|
|
_startSymbol = startSymbol;
|
|
|
|
|
}
|
|
|
|
|
public int SelectedIndex
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (listBoxObjects.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return listBoxObjects.SelectedIndex;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (listBoxObjects.SelectedItems.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
listBoxObjects.SelectedIndex = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public T GetObjectFromStr<T>() where T : class, new()
|
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
if (listBoxObjects.SelectedIndex == -1 || string.IsNullOrEmpty(_layout) || !_startSymbol.HasValue || !_endSymbol.HasValue)
|
|
|
|
|
throw new ArgumentException("Не хватает данных");
|
|
|
|
|
|
|
|
|
|
var type = typeof(T);
|
|
|
|
|
var fields = type.GetFields();
|
|
|
|
|
var properties = type.GetProperties();
|
|
|
|
|
var members = fields.Cast<MemberInfo>().Concat(properties.Cast<MemberInfo>()).ToArray();
|
|
|
|
|
|
|
|
|
|
var curObject = new T();
|
|
|
|
|
string text = listBoxObjects.SelectedItem?.ToString() ?? "";
|
|
|
|
|
|
|
|
|
|
var words = System.Text.RegularExpressions.Regex.Split(_layout, $@"\{_startSymbol.Value}.*?\{_endSymbol.Value}");
|
|
|
|
|
|
|
|
|
|
int firstWordStart = text.IndexOf(words[0], 0);
|
|
|
|
|
if (firstWordStart == -1)
|
|
|
|
|
throw new Exception("Не найден элемент шаблона");
|
|
|
|
|
if (firstWordStart != 0)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
string beginning = text[..firstWordStart];
|
|
|
|
|
FillMember(_layout.Substring(1, firstWordStart - 2), curObject, beginning, members);
|
2024-09-22 20:37:12 +04:00
|
|
|
|
}
|
2024-10-04 11:08:55 +04:00
|
|
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < words.Length - 1; i++)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
start = text.IndexOf(words[i], start);
|
|
|
|
|
if (start == -1)
|
|
|
|
|
throw new Exception("Не найден элемент шаблона");
|
|
|
|
|
start += words[i].Length;
|
|
|
|
|
|
|
|
|
|
int nextWordIndex = text.IndexOf(words[i + 1], start);
|
|
|
|
|
if (nextWordIndex == -1)
|
|
|
|
|
throw new Exception("Не найден следующий элемент шаблона");
|
|
|
|
|
|
|
|
|
|
string valueBetween = text[start..nextWordIndex];
|
|
|
|
|
|
|
|
|
|
string layoutPart = _layout.Substring(_layout.IndexOf(words[i]) + words[i].Length);
|
|
|
|
|
int startCharIndex = layoutPart.IndexOf(_startSymbol.Value);
|
|
|
|
|
int endCharIndex = layoutPart.IndexOf(_endSymbol.Value);
|
|
|
|
|
string memberName = layoutPart.Substring(startCharIndex + 1, endCharIndex - startCharIndex - 1);
|
|
|
|
|
|
|
|
|
|
FillMember(memberName, curObject, valueBetween, members);
|
|
|
|
|
|
|
|
|
|
start = nextWordIndex;
|
2024-09-22 20:37:12 +04:00
|
|
|
|
}
|
2024-10-04 11:08:55 +04:00
|
|
|
|
|
|
|
|
|
return (T?)curObject;
|
|
|
|
|
|
2024-09-22 20:37:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 11:08:55 +04:00
|
|
|
|
public void AddItems<T>(List<T> items)
|
|
|
|
|
where T : class
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
if (string.IsNullOrEmpty(_layout) || !_startSymbol.HasValue || !_endSymbol.HasValue)
|
|
|
|
|
throw new ArgumentException("Не хватает данных");
|
|
|
|
|
listBoxObjects.Items.Clear();
|
|
|
|
|
var type = typeof(T);
|
|
|
|
|
var fields = type.GetFields();
|
|
|
|
|
var properties = type.GetProperties();
|
|
|
|
|
var members = fields.Cast<MemberInfo>().Concat(properties.Cast<MemberInfo>()).ToArray();
|
|
|
|
|
foreach (T item in items)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
string result = _layout;
|
|
|
|
|
foreach (var member in members)
|
|
|
|
|
{
|
|
|
|
|
string search = _startSymbol.Value + member.Name +_endSymbol.Value;
|
|
|
|
|
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() ?? "");
|
|
|
|
|
}
|
|
|
|
|
listBoxObjects.Items.Add(result);
|
2024-09-22 20:37:12 +04:00
|
|
|
|
}
|
2024-10-04 11:08:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
property.SetValue(obj, value);
|
2024-09-22 20:37:12 +04:00
|
|
|
|
}
|
2024-10-04 11:08:55 +04:00
|
|
|
|
else if (member is FieldInfo field)
|
2024-09-22 20:37:12 +04:00
|
|
|
|
{
|
2024-10-04 11:08:55 +04:00
|
|
|
|
field.SetValue(obj, value);
|
2024-09-22 20:37:12 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-04 11:08:55 +04:00
|
|
|
|
|