Indirectly changing class parameters

I am showing a class in TreeListView ( ObjectListView ) using reflection. TreeListView works with a delegate ChildrenGetter()that receives the displayed nodes (see here ).

To map a class (we will call it a reflected class), the root of the tree is an instance of the class, and ChildrenGetter(object x)uses reflection to return instances of each child of the reflected class.

For example, for this class:

public class Folder
{
    public int BuildNumber { get; set; }
    public int MaxSpeed { get; set; }
}

The root will be an instance of the Folder, and it will have 2 child nodes containing the values ​​BuildNumber and MaxSpeed.

This works fine, but only for display. I would like to be able to change the values ​​of BuildNumber and MaxSpeed ​​in the user interface so that they really change in the instance of the reflected class stored in the root of the tree.

So there are two ways to do this:

  • Save the parent (owning class) from each node in the node tree and create an “address path” for each node, then using reflection, find the changed node and actually change it (to an instance of the reflected class)
  • Instead of the nodes of the tree containing the values ​​of the parameters of the reflected class, they may contain some kind of pointer to the parameters in the instance of the reflected class.

Is solution 2 possible? Is there any equivalent to a pointer in C # that might allow such functionality?

Can you come up with a better solution?

Notes:

  • , , . structs where
  • .
  • , ,
+4
1

/, Action<T> - .

, - ( string ):

Action<string> action = val => instance.X = val;
+1

Source: https://habr.com/ru/post/1620117/


All Articles