fix: поправил сборку объекта в TreeView, теперь поля могут быть любого типа

This commit is contained in:
Никита Потапов 2024-11-19 23:44:02 +04:00
parent 176ba5e313
commit 7e50fda55f
3 changed files with 9 additions and 5 deletions

View File

@ -49,7 +49,7 @@ namespace Components
int propIndex = GetNodeDepth(node);
while (node != null)
{
string propValue = node.Text;
object propValue = node.Tag;
string propName = Hierarchy[propIndex].PropertyName;
var prop = obj.GetType().GetProperty(propName);
@ -100,7 +100,7 @@ namespace Components
{
throw new PropertyNotDeclaratedException(hierarchyProperty.PropertyName);
}
string? objectPropertyValue = objectPropertyInfo.GetValue(obj)?.ToString() ?? null;
object? objectPropertyValue = objectPropertyInfo.GetValue(obj);
if (objectPropertyValue == null)
{
throw new PropertyNullException(hierarchyProperty.PropertyName);
@ -112,7 +112,7 @@ namespace Components
{
foreach (TreeNode childNode in nodes)
{
if (childNode.Text == objectPropertyValue)
if (childNode.Text == objectPropertyValue.ToString())
{
node = childNode;
break;
@ -122,7 +122,8 @@ namespace Components
if (node == null)
{
node = nodes.Add(objectPropertyValue);
node = nodes.Add(objectPropertyValue.ToString());
node.Tag = objectPropertyValue;
}
nodes = node.Nodes;

View File

@ -2,6 +2,7 @@
{
public class Employee
{
public int Id { get; set; }
public string Departament { get; set; }
public string Position { get; set; }
public string Name { get; set; }
@ -9,6 +10,7 @@
public Employee() { }
public Employee(string departament, string position, string surname, string name)
{
Id = new Random().Next(4);
Departament = departament;
Position = position;
Name = name;
@ -16,7 +18,7 @@
}
public override string ToString()
{
return Departament + ", " + Position + ", " + Surname + " " + Name;
return Id.ToString() + ", " + Departament + ", " + Position + ", " + Surname + " " + Name;
}
}
}

View File

@ -47,6 +47,7 @@ namespace WinFormsApp
List<(string PropertyName, bool AlwaysCreateBranch)> hierarchy =
[
("Id", false),
("Departament", false),
("Position", false),
("Surname", false),