如何转换 TreeNodeCollection
至List<TreeNode>
?
这不起作用。
ICollection tnc = treeview1.Nodes;
ICollection<TreeNode> tn = (ICollection<TreeNode>)tnc;
List<TreeNode> list = new List<TreeNode>(tn);
异常:
Unable to cast object of type 'System.Windows.Forms.TreeNodeCollection' to type 'System.Collections.Generic.ICollection`1[System.Windows.Forms.TreeNode]'.
最佳答案
一种解决方案是:
List<TreeNode> treeNodeList = treeNodeCollection.OfType<TreeNode>().ToList();
另一个:
List<TreeNode> treeNodeList = treeNodeCollection.Cast<TreeNode>().ToList();
foreach 循环:
List<TreeNode> treeNodeList = new List<TreeNode>();
foreach (TreeNode item in treeNodeCollection)
treeNodeList.Add(item);
关于c# - 将 TreeNodeCollection 转换为 List<TreeNode>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50499099/