如何在C#中递归构建TreeView

发布于 2024-09-24 02:11:59 字数 982 浏览 1 评论 0 原文

private void enumerateValues(IEnumerator<KeyValuePair<string, object>> iEnumerator, TreeNode parentnode)
{
   if(iEnumerator is IEnumerable)
   {
     // ADD THE KEY
     TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Key);

     enumerateValues(iEnumerator.Current.Value, childNode);
   }
  else
   {
     /// add the value
     TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Value.ToString());
   }
}

我不知何故收到以下错误:

最佳重载方法匹配 'WindowsFormsApplication1.Form1.enumerateValues(System.Collections.Generic.IEnumerator>, System.Windows.Forms.TreeNode)' 有一些无效参数
参数“1”:无法从“object”转换为“System.Collections.Generic.IEnumerator>”

请问我该如何修复它

private void enumerateValues(IEnumerator<KeyValuePair<string, object>> iEnumerator, TreeNode parentnode)
{
   if(iEnumerator is IEnumerable)
   {
     // ADD THE KEY
     TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Key);

     enumerateValues(iEnumerator.Current.Value, childNode);
   }
  else
   {
     /// add the value
     TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Value.ToString());
   }
}

I somehow get the following error:

The best overloaded method match for 'WindowsFormsApplication1.Form1.enumerateValues(System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,object>>,
System.Windows.Forms.TreeNode)' has some invalid arguments
Argument '1': cannot convert from 'object' to 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,object>>'

How can i fix it please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-10-01 02:11:59

以下行很可能是罪魁祸首:

enumerateValues(iEnumerator.Current.Value, childNode);

因为 enumerateValues 方法接受 IEnumerator>,因此键值对的值将始终属于对象类型。因此,您无法使用 iEnumerator.Current.Value 调用该方法,因为该值不是 IEnumerator> 类型。

这正是错误消息告诉您的内容:

参数“1”:无法从“object”转换为“System.Collections.Generic.IEnumerator>”

您必须先将 iEnumerator.Current.Value 转换为正确的类型,然后才能调用该方法。您可以使用as运算符

private void enumerateValues(IEnumerator<KeyValuePair<string, object>> iEnumerator, TreeNode parentnode)
{
  // Loop through the values.
  while (iEnumerator.MoveNext())
  {
    // Try a 'safe' cast of the current value.
    // If the cast fails, childEnumerator will be null.
    var childEnumerator = iEnumerator.Current.Value as IEnumerator<KeyValuePair<string, object>>;

    if (childEnumerator != null)
    {
      TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Key);

      enumerateValues(childEnumerator, childNode);
    }
    else
    {
      TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Value.ToString());
    }
  }
}

我还建议您使用 IEnumerable<如果可以的话,请使用 T> 而不是 IEnumerator。它更清楚地显示代码的意图,您不必手动处理迭代,您可以使用 LINQ就可以了。

private void enumerateValues(IEnumerable<KeyValuePair<string, object>> items, TreeNode parentnode)
{
  foreach (var item in items)
  {
    // Try a 'safe' cast of the current value.
    // If the cast fails, childEnumerator will be null.
    var childEnumerator = item.Value as IEnumerable<KeyValuePair<string, object>>;

    if (childEnumerator != null)
    {
      TreeNode childNode = parentnode.Nodes.Add(item.Key);

      enumerateValues(childEnumerator, childNode);
    }
    else
    {
      TreeNode childNode = parentnode.Nodes.Add(item.Value.ToString());
    }
  }
}

The following line is most likely the culprit:

enumerateValues(iEnumerator.Current.Value, childNode);

Because the enumerateValues method accepts a IEnumerator<KeyValuePair<string, object>>, the values of the key-value pairs will always be of type object. Therefore you cannot call the method with iEnumerator.Current.Value, because the value isn't of type IEnumerator<KeyValuePair<string, object>>.

This is exactly what the error message tells you:

Argument '1': cannot convert from 'object' to 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,object>>'

You will have to cast iEnumerator.Current.Value to the correct type first, before you can call the method. You can do this using the as operator.

private void enumerateValues(IEnumerator<KeyValuePair<string, object>> iEnumerator, TreeNode parentnode)
{
  // Loop through the values.
  while (iEnumerator.MoveNext())
  {
    // Try a 'safe' cast of the current value.
    // If the cast fails, childEnumerator will be null.
    var childEnumerator = iEnumerator.Current.Value as IEnumerator<KeyValuePair<string, object>>;

    if (childEnumerator != null)
    {
      TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Key);

      enumerateValues(childEnumerator, childNode);
    }
    else
    {
      TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Value.ToString());
    }
  }
}

I also suggest you use IEnumerable<T> instead of IEnumerator<T>, if you can. It shows the intent of the code more clearly, you won't have to handle the iteration manually and you can use LINQ on it.

private void enumerateValues(IEnumerable<KeyValuePair<string, object>> items, TreeNode parentnode)
{
  foreach (var item in items)
  {
    // Try a 'safe' cast of the current value.
    // If the cast fails, childEnumerator will be null.
    var childEnumerator = item.Value as IEnumerable<KeyValuePair<string, object>>;

    if (childEnumerator != null)
    {
      TreeNode childNode = parentnode.Nodes.Add(item.Key);

      enumerateValues(childEnumerator, childNode);
    }
    else
    {
      TreeNode childNode = parentnode.Nodes.Add(item.Value.ToString());
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文