【问题标题】:Accessing object properties from a treenode that's associated with it?从与其关联的树节点访问对象属性?
【发布时间】:2010-06-07 04:01:09
【问题描述】:

你好, 我读过Easy object binding to Treeview Node, 但仍有未解决的问题。

如果一个对象与树节点标签属性相关联,如何从该树节点访问该对象成员/属性?


node1 = new TreeNode();
node1.tag = object1;
//ex:if object1 has public property valueA
//How to access valueA  from node1 ??

【问题讨论】:

    标签: c# treenode


    【解决方案1】:

    也许您可以将其转换回 object1 类型...

    var valueA = ((object1Type)node1.tag).valueA;
    

    【讨论】:

      【解决方案2】:
      MyClass c = treeNode.Tag as MyClass;
      theValue = c.TheProperty;
      

      如果你不知道有问题的对象的类型,那么你可以使用 System.Reflection:

      System.Reflection.PropertyInfo pi = treeNode.Tag.GetType().GetProperty("SomeName");
      theValue = pi.GetValue(treeNode.Tag, null);
      

      最后,如果您想知道属性的名称,请再次使用 System.Reflection 进行救援:

      System.Reflection.PropertyInfo[] pis = treeNode.Tage.GetType().GetProperties();
      foreach (var pi in pis) {
        theName = pi.Name;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        相关资源
        最近更新 更多