简体   繁体   English

为什么我的委托作为参数传递给方法时不起作用?

[英]Why does my delegate not work when passed in as a parameter to a method?

I created a delegate and i want to use the delegate as a parameter in a method parameter list. 我创建了一个委托,我想使用委托作为方法参数列表中的参数。 I think want to call handler just like I did in the Main method which work perfectly. 我想要像在Main方法中那样完美地调用处理程序。

Question: How can I pass a delegate to a method? 问题:如何将委托传递给方法?

Code: 码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        public delegate void Del(string e);
        Del handler = DelegateMethod;

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)     <--Write Here!!!
        {
            d.handler("33");
        }


        static void Main(string[] args)
        {
            // Instantiate the delegate.
            // Del handler = DelegateMethod;

            Program p = new Program();
            p.handler("Hello World");    <--Like this example here (these work)
            p.handler("DisneyLand");
            p.handler("Cattle Wars");

            testDel(p.handler("Cattle Wars");
        }
    }
}

Error List: 错误列表:

Error   1   The best overloaded method match for 'ConsoleApplication1.Program.testDel(ConsoleApplication1.Program.Del)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  12  ConsoleApplication1
Error   2   Argument 1: cannot convert from 'void' to 'ConsoleApplication1.Program.Del' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  20  ConsoleApplication1
Error   3   ) expected  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  44  ConsoleApplication1

Working Code: 工作守则:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public delegate void Del(string e);
        Del handler = DelegateMethod;

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)
        {
            d.Invoke("L");
        }

        static void Main(string[] args)
        {
            // Instantiate the delegate.
            // Del handler = DelegateMethod;

            Program p = new Program();
            p.handler("Hello World");
            p.handler("DisneyLand");
            p.handler("Cattle Wars");
            p.testDel(p.handler);
        }
    }
}

In the line 在线

testDel(p.handler("Cattle Wars"));

p.handler is invoked. 调用p.handler There is no longer a Del to pass into testDel . Del不再传入testDel Maybe you were looking for: 也许您在寻找:

testDel(p.handler);

This passes the Del so that testDel can invoke it. 这会传递Del以便testDel可以调用它。

You have to pass the delegate into the method with out the "Cattle Wars" parameter 您必须将委托传递给方法而不使用“Cattle Wars”参数

   testDel(p.handler);

Then you method should look like the following. 那么你的方法应该如下所示。

    public void testDel(Del d)
    {
        d.Invoke("L");
    }

I will have to say that delegates still seem strange to me. 我不得不说,代表们对我来说仍然很奇怪。 I know that they are just variables that are type safe and thread safe. 我知道它们只是类型安全且线程安全的变量。 You can use the variable just like calling the method name. 您可以像调用方法名一样使用变量。

    public delegate void Del(string e);
    Del handler = DelegateMethod;

In your example here your delegate can be set to any method that has a return type of void and takes one argument that is a string. 在您的示例中,您的委托可以设置为任何返回类型为void的方法,并且接受一个字符串参数。

   Del handler = DelegateMethod;

The above is where you declare you delegate but you also can declare the delegate to any methods that excepts void and one parameter as a string. 上面是您声明委托的地方,但您也可以将委托声明为除了void和一个参数之外的任何方法作为字符串。

    handler = BaseBall;


     public void BaseBall(string a) 
     {
         //code here
     }

I would just keep reading you will learn as you go. 我会继续读你会随时学习。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在作为参数传递时收集了委托函数引用垃圾? - Why is a delegate function reference garbage collected when passed as parameter? 使用作为方法参数传递的参数进行委托-如何访问委托arg? - Delegate with parameter passed as a method parameter - how to access the delegate arg? 为什么在未指定参数列表的情况下委托使用参数? - Why does the delegate take a parameter when no parameter list is specified? 当委托作为通用参数传递时调用C#委托方法 - Invoking C# Delegate Method when delegate is passed as Generic Argument 为什么SqlConnection作为参数传递时使用引用类型? - Why does SqlConnection use reference type when passed as parameter? 为什么作为参数传递时属性变成null? - Why does the property become null when passed as a parameter? 获取作为参数传递的委托的方法名称(紧凑框架) - Get method name of a delegate passed as a parameter (Compact Framework) 当我仅登录一个用户时,我的方法SendFcmNativeNotificationAsync不起作用,哪个参数是发送该参数的正确参数? - My method SendFcmNativeNotificationAsync does not work when I login for only one user, which parameter is the correct one to send it? 为什么IList的UpdateModel()绑定不起作用,但是作为方法参数起作用? - Why UpdateModel() binding of IList does not work, but as a method parameter it does? 为什么我的disableButtons方法不起作用? - Why does my disableButtons method not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM