Change fields to properties

This commit is contained in:
abazov73 2023-11-30 00:55:32 +04:00
parent 105c89cda0
commit 295edcaaa9
2 changed files with 11 additions and 11 deletions

View File

@ -38,16 +38,16 @@ namespace AbazovViewComponents.Components
TreeNode? node = treeView.SelectedNode;
var type = typeof(T);
var fields = type.GetFields();
var properties = type.GetProperties();
var item = Activator.CreateInstance(type);
while (node != null)
{
var field = fields.FirstOrDefault(x => x.Name == node.Name);
if (field != null)
var property = properties.FirstOrDefault(x => x.Name == node.Name);
if (property != null)
{
field.SetValue(item, node.Text);
property.SetValue(item, node.Text);
}
node = node.Parent;
}
@ -63,23 +63,23 @@ namespace AbazovViewComponents.Components
public void addItems<T>(List<T> items)
{
var type = typeof(T);
var fields = type.GetFields();
var properties = type.GetProperties();
foreach (T item in items)
{
TreeNodeCollection nodes = treeView.Nodes;
for (int i = 0; i < hierarchy.Count; i++)
{
var field = fields.FirstOrDefault(x => x.Name.Equals(hierarchy[i].Item1));
if (field is not null)
var property = properties.FirstOrDefault(x => x.Name.Equals(hierarchy[i].Item1));
if (property is not null)
{
var node = nodes.Find(field.Name, false).FirstOrDefault(x => x.Text == field.GetValue(item).ToString());
var node = nodes.Find(property.Name, false).FirstOrDefault(x => x.Text == property.GetValue(item).ToString());
if (node is not null && !hierarchy[i].Item2)
{
nodes = node.Nodes;
}
else
{
TreeNode newNode = nodes.Add(field.Name, field.GetValue(item).ToString());
TreeNode newNode = nodes.Add(property.Name, property.GetValue(item).ToString());
nodes = newNode.Nodes;
}
}

View File

@ -32,8 +32,8 @@ namespace AbazovViewComponents.LogicalComponents
{
Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
FieldInfo? seriesName = typeof(T).GetField(seriesNameField);
FieldInfo? value = typeof(T).GetField(valueField);
PropertyInfo? seriesName = typeof(T).GetProperty(seriesNameField);
PropertyInfo? value = typeof(T).GetProperty(valueField);
if (seriesName == null || value == null) throw new ArgumentException("Переданного поля не существует");
int columnCount = 2;
foreach(var item in data)