简体   繁体   English

委托声明和声明委托的类之间的关系是什么

[英]what is the relation between delegate declaration and the class in which the delegate is declared

I'm very curious to know why the delegate type is visible in intellisense when I type a dot after the class name in my program file and the delegate name appears after the class's object name. 我很好奇,为什么我在程序文件中的类名之后键入点并在类的对象名之后出现委托名称时,为什么在智能感知中可以看到委托类型。
Consider the following code 考虑以下代码

public class DelDec
{
    public delegate void Initdel(String s);
    public Initdel Init;
}

If I have a instancemethod in another class 如果我在另一个课程中有一个实例方法

public class Methoddef
{
    public void process(string s)
    {
        Console.WriteLine("The message passed is" + s);
    }
}

Now when 现在,当

class Program
{
    static void Main(string[] args)
    {
        Methoddef md=new Methoddef();
        DelDec.Initdel d = md.process;
    }
}  

My concern is demonstrated with the following images 下图显示了我的关注 在此处输入图片说明

在此处输入图片说明

I really want to clarify this concept. 我真的很想澄清这个概念。

A delegate like this is just a nested class extending the special System.MulticastDelegate type and with runtime defined methods. 这样的委托仅仅是一个嵌套的类,它扩展了特殊的System.MulticastDelegate类型并具有运行时定义的方法。 Have a look at the IL code, produced by compiling this c# code: 看一下通过编译此c#代码产生的IL代码:

public class C {
    public delegate void Del();
}


.class public auto ansi beforefieldinit C
    extends [mscorlib]System.Object
{
    // Nested Types
    .class nested public auto ansi sealed Del
        extends [mscorlib]System.MulticastDelegate
    {
        // Methods
        .method public hidebysig specialname rtspecialname 
            instance void .ctor (
                object 'object',
                native int 'method'
            ) runtime managed 
        {
        } // end of method Del::.ctor

        .method public hidebysig newslot virtual 
            instance void Invoke () runtime managed 
        {
        } // end of method Del::Invoke

        .method public hidebysig newslot virtual 
            instance class [mscorlib]System.IAsyncResult BeginInvoke (
                class [mscorlib]System.AsyncCallback callback,
                object 'object'
            ) runtime managed 
        {
        } // end of method Del::BeginInvoke

        .method public hidebysig newslot virtual 
            instance void EndInvoke (
                class [mscorlib]System.IAsyncResult result
            ) runtime managed 
        {
        } // end of method Del::EndInvoke

    } // end of class Del

} // end of class C

Note the runtime keyword at the method declaration, meaning that the method implementation will be provided by the runtime itself. 注意方法声明中的runtime关键字,这意味着方法实现将由运行时本身提供。

So what you are seeing in intellisense is just the presence of the nested type, the same should happen for any other nested type. 因此,您在intellisense中看到的只是嵌套类型的存在,其他任何嵌套类型也应如此。

You declare method signature and name of delegate type, when you declare delegate in class. 在类中声明委托时,可以声明方法签名和委托类型的名称。 For example you know that you want call this method from you object with some signature, but you don't know which one, so you can it assign programmaticaly. 例如,您知道要从带有某些签名的对象中调用此方法,但是您不知道哪个签名,因此可以对其进行编程分配。

Here is code which can help you to understand. 这是可以帮助您理解的代码。

class Program
{
    static void Main(string[] args)
    {
        Methoddef md=new Methoddef();
        DelDec.Initdel d = md.process; //you create variable of delegate type Initdel and asign this variable

        var obj = new DelDec();
        obj.Init = d; //assign delegate to class instance

        obj.Init("Test");

    }

    public class DelDec
    {
        //Declares a delegate for a void method that takes an string parameter.
        public delegate void Initdel(String s);
        public Initdel Init;
    }


    public class Methoddef
    {
        public void process(string s)
        {
            Console.WriteLine("The message passed is " + s);
        }
    }
}

Also good if you read this https://msdn.microsoft.com/ru-ru/library/system.delegate(v=vs.110).aspx 如果您阅读此https://msdn.microsoft.com/ru-ru/library/system.delegate(v=vs.110).aspx,也很好

So in your first picture DelDec.Initdel - delagate type, in your second picrure obj.Initdel it is field where you can assign come method to delegate. 因此,在您的第一张图片DelDec.Initdel -滞后类型,在第二张图片obj.Initdel ,您可以在此字段中分配come方法来进行委派。

I hope that my answer will help you. 希望我的回答对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM