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

View File

@ -2,6 +2,7 @@
{ {
public class Employee public class Employee
{ {
public int Id { get; set; }
public string Departament { get; set; } public string Departament { get; set; }
public string Position { get; set; } public string Position { get; set; }
public string Name { get; set; } public string Name { get; set; }
@ -9,6 +10,7 @@
public Employee() { } public Employee() { }
public Employee(string departament, string position, string surname, string name) public Employee(string departament, string position, string surname, string name)
{ {
Id = new Random().Next(4);
Departament = departament; Departament = departament;
Position = position; Position = position;
Name = name; Name = name;
@ -16,7 +18,7 @@
} }
public override string ToString() 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 = List<(string PropertyName, bool AlwaysCreateBranch)> hierarchy =
[ [
("Id", false),
("Departament", false), ("Departament", false),
("Position", false), ("Position", false),
("Surname", false), ("Surname", false),