What is the correct way to call an extension metho

2019-08-24 19:38发布

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);
    }
}

2条回答
看我几分像从前
2楼-- · 2019-08-24 19:53

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.

    }
}
查看更多
狗以群分
3楼-- · 2019-08-24 19:56

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);
}
查看更多
登录 后发表回答