代表方法的名称。 [英] Delegates name of method.

查看:134
本文介绍了代表方法的名称。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

namespace delegateexample
{
    delegate int somedelegate(int i);
    class someclass
    {
        public static int somemethod(int i)
        {
            return i * 2;
        }
        public static int somemethod2(int i)
        {
            return i / 2;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            somedelegate process = new somedelegate(someclass.somemethod());
            process(4);

        }
    }
}

我有一些代码。如果有一些方法在课程中说明我可以写这个方法的名字就像这样

I have some code.If somemethod is stated in class Program i can write name of this method like this

somedelegate process = new somedelegate(somemethod);

但如何在委托中编写方法,如果此方法位于其他类中。

But how to write method in delegate,if this method situated in other class.

推荐答案

如果我理解你的问题,你几乎可以在上面的代码中弄清楚:

If I understand your question, you've almost got it correct in your code above:
class Program
{
    static void Main(string[] args)
    {
        somedelegate process = new somedelegate(someclass.somemethod);  // NOTE: no parens ()
        process(4);
    }
}


解决方案1仅适用于静态方法。是的,你问过静态方法,但这不是唯一的可能性。但是这也可以用于类实例。比较:

Solution 1 is only good for static methods. Yes, you asked about static methods, but this is not the only possibility. But this is also possible with the class instance. Compare:
delegate int SomeDelegate(int i);

class Implementation {
    internal int SomeMethod(int i) { /* ... */ return i; }
    internal static int SomeOtherMethod(int i) { /* ... */ return i; }
}

class ImplementationUsage {
    internal void Demo() {
        SomeDelegate theDelegateInstance =  Implementation.SomeOtherMethod;
        theDelegateInstance(3);
        Implementation implementation = new Implementation();
        theDelegateInstance = implementation.SomeMethod;
        theDelegateInstance(4);
    }
}



注意我完全删除 new SomeDelegate(/ * ... * /); 这是一个完全冗余的表达式,在这种情况下(原始MSDN文档可能不清楚)。

此外,您不必声明委托 int SomeDelegate(int i); 相反,你可以根据 System.Func 的集合使用已经可用的声明泛型声明: System.Func< int,int>



委托实例类(不要与委托类型混淆)是这样一种结构,它可以带有两个引用:要调用的方法,以及实例在调用时隐式传递给此方法的类型。事实上,这是更先进的类型,很难详细解释。对于某些人,请参阅我的文章4.1代表实例的性质的相应部分:动态方法调度程序 [ ^ ]。



值得一提的是,实现委托的另一种非常有用的方法是使用匿名方法。由于它们的多重好处,您必须很好地了解它们,特别是对于事件处理,还有许多其他情况。 (如果您对它们有任何疑问,我很乐意回答。)



-SA


Pay attention that I completely removed new SomeDelegate(/* ... */); this is a totally redundant expression, in this case (and original MSDN documentation is probably not clear about it).
Also, you don't have to declare delegate int SomeDelegate(int i); instead, you can use already available declaration based on the set of System.Func generic declarations: System.Func<int, int>.

The delegate instance class (not to be confused with delegate type) is such a structure which can carry two references: to the method to be called, and to the instance of the type implicitly passed to this method at the moment of the call. In fact, this is much more advanced type, which is pretty hard to explain in all detail. For some, please see the corresponding section of my article, "4.1 On the Nature of Delegate Instance": Dynamic Method Dispatcher[^].

It's worth mentioning that another, very useful way of implementing delegates is using anonymous methods. You have to know them well due to multiple benefits of them, especially for event handling, but also many other cases. (I'll gladly answer if you have any questions on them.)

—SA


这篇关于代表方法的名称。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆