What is the correct way to call an extension method (TreeNodeCollection Add method)?

醉酒当歌 提交于 2019-12-11 16:04:17

问题


The pertinent parts of my code are below. In the MyTreeView class (last block of code below), the line of code TncExtensions.TncNodeAdd(this, myTreeViewNode); generates the error CS7036 There is no argument given that corresponds to the required formal parameter 'myTreeViewNode' of 'TncExtensions.TncNodeAdd(TreeNodeCollection, MyTreeView_Abstract, MyTreeViewNode_Abstract)'

Why can't the compiler figure out what the 2nd formal parameter is for my TreeNodeCollection extension method?

public static class TncExtensions
{
    public static int TncNodeAdd(this TreeNodeCollection nodes, MyTreeView_Abstract myTreeView, MyTreeViewNode_Abstract myTreeViewNode)
    {
        return myTreeView.Nodes.Add(myTreeViewNode);
    }
}

public abstract class MyTreeViewNode_Abstract : TreeNode
{
    public MyTreeViewNode_Abstract(string text) : base(text)
    {
    }
}

public class MyTreeViewNode : MyTreeViewNode_Abstract
{
    public MyTreeViewNode(string text) : base(text)
    {
    }
}

public abstract class MyTreeView_Abstract : TreeView
{
}

public class MyTreeView : MyTreeView_Abstract
{
    public void CreateTree()
    {
        MyTreeViewNode myTreeViewNode = new MyTreeViewNode("node text");
        TncExtensions.TncNodeAdd(this, myTreeViewNode);
    }
}

回答1:


I believe the extension method which you created is not of much use but if you are curious to know what the problem is, you are using the extension method wrong. You are accessing it like a static method, in this style you need to pass 3 arguments based on the signature of the method:

TncExtensions.TncNodeAdd(Nodes, this, myTreeViewNode);

Or use it like an extension method:

this.Nodes.TncNodeAdd(this, myTreeViewNode);

I suggest you change the extension method to:

public static int TncNodeAdd(this TreeNodeCollection nodes, MyTreeViewNode_Abstract myTreeViewNode)
{
    return nodes.Add(myTreeViewNode);
}

To learn more, take a look at Extension Methods.




回答2:


Reza'a answer is still the answer, but to make my treenode Add extension method useful, I had to modify my calls to it. I forgot that as part of adding a treenode, you need to specify what node collection you want to append the new node to.

Note: See code in original question for the basic classes involved

Modified Add extension method as suggested by Reza

namespace TreeNodeCollectionExtensions
{
    public static class TncExtensions
    {
        public static int Add(this TreeNodeCollection nodes, DRT.DRT_TvwNode_Abstract myTreeViewNode)
        {
            int newNodeIndex = nodes.Add(myTreeViewNode);

            //Do stuff to custom properties that are members of DRT_TvwNode_Abstract and classes derived from DRT_TvwNode_Abstract

            return newNodeIndex;
        }
    }
}

Use it like this

using TreeNodeCollectionExtensions;

public class MyTreeView : MyTreeView_Abstract
{
    public MyTreeView() : base()
    {
    }

    public void CreateTree()
    {
        MyTreeViewNode myTreeViewNode;

        //Add node to root of TreeView 
        //Using named parameter to force the compiler to
        //use the Add extension method and not the base Add method
        myTreeViewNode = new MyTreeViewNode("root node text");
        Nodes.Add(myTreeViewNode: myTreeViewNode);

        //Add node one level below root node of TreeView
        //Using named parameter to force the compiler to
        //use the Add extension method and not the base Add method
        myTreeViewNode = new MyTreeViewNode("level 1 node text");
        int newLevel1NodeIndex = Nodes[0].Nodes.Add(myTreeViewNode: myTreeViewNode);

        //Add node one level below level 1 node just created 
        //Using named parameter to force the compiler to
        //use the Add extension method and not the base Add method
        myTreeViewNode = new MyTreeViewNode("level 2 node text");
        int newLevel2NodeIndex = Nodes[0].Nodes[newLevel1NodeIndex].Nodes.Add(myTreeViewNode: myTreeViewNode);

        //etc., etc.

    }
}


来源:https://stackoverflow.com/questions/51117529/what-is-the-correct-way-to-call-an-extension-method-treenodecollection-add-meth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!