2016-09-03 2 views
0

Ich habe gerade angefangen, Delegierte in C# zu lernen. Wie das Delegat-Objekt erstellt wird, wenn der Compiler den folgenden CodeWie die Delegiertenobjekte in C# erstellt werden?

delegate int Transformer (int x); 
Transformer t = new Transformer (Square); 

ich, dass alle Delegierten implizit die System.Delegate-Klasse abgeleitet gefunden begegnen. Wird der "neue Transformer (Square)" den Konstruktor der Delegate-Klasse aufrufen, um das Objekt "t" zu erstellen.

Antwort

0

Im Rückblick auf Roslyn-Referenzquellen für .NET Framework 4.6.2 Die System.Delegate Klassenkonstruktors wie folgt aussieht:

[System.Security.SecuritySafeCritical] // auto-generated 
     protected Delegate(Object target,String method) 
     { 
      if (target == null) 
       throw new ArgumentNullException("target"); 

      if (method == null) 
       throw new ArgumentNullException("method"); 
      Contract.EndContractBlock(); 

      // This API existed in v1/v1.1 and only expected to create closed 
      // instance delegates. Constrain the call to BindToMethodName to 
      // such and don't allow relaxed signature matching (which could make 
      // the choice of target method ambiguous) for backwards 
      // compatibility. The name matching was case sensitive and we 
      // preserve that as well. 
      if (!BindToMethodName(target, (RuntimeType)target.GetType(), method, 
            DelegateBindingFlags.InstanceMethodOnly | 
            DelegateBindingFlags.ClosedDelegateOnly)) 
       throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); 
     } 

     // This constructor is called from a class to generate a 
     // delegate based upon a static method name and the Type object 
     // for the class defining the method. 
     [System.Security.SecuritySafeCritical] // auto-generated 
     protected unsafe Delegate(Type target,String method) 
     { 
      if (target == null) 
       throw new ArgumentNullException("target"); 

      if (target.IsGenericType && target.ContainsGenericParameters) 
       throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), "target"); 

      if (method == null) 
       throw new ArgumentNullException("method"); 
      Contract.EndContractBlock(); 

      RuntimeType rtTarget = target as RuntimeType; 
      if (rtTarget == null) 
       throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target"); 

      // This API existed in v1/v1.1 and only expected to create open 
      // static delegates. Constrain the call to BindToMethodName to such 
      // and don't allow relaxed signature matching (which could make the 
      // choice of target method ambiguous) for backwards compatibility. 
      // The name matching was case insensitive (no idea why this is 
      // different from the constructor above) and we preserve that as 
      // well. 
      BindToMethodName(null, rtTarget, method, 
          DelegateBindingFlags.StaticMethodOnly | 
          DelegateBindingFlags.OpenDelegateOnly | 
          DelegateBindingFlags.CaselessMatching); 
     } 

Also, ja Sie einen abgeleiteter Typen von diesem Kerl erwarten.

Wenn Sie Grundlagen auf Teilnehmer freuen: (Ich glaube, ich die Frage falsch verstanden, noch hier zu halten)

Syntax für Delegatendeklaration ist

delegate <return type> <delegate-name> <parameter list> 

From MSDN:

Ein Delegierter ist ein Typ, der Verweise auf Methoden mit einer bestimmten Parameterliste und einem Rückgabetyp darstellt. Wenn Sie einen Delegaten instanziieren, können Sie seine Instanz einer beliebigen Methode mit einer kompatiblen Signatur und einem Rückgabetyp zuordnen. Sie können die Methode über die Delegateninstanz aufrufen (oder aufrufen).

Sobald ein Delegattyp deklariert ist, muss daher ein Delegatobjekt mit dem neuen Schlüsselwort erstellt und einer bestimmten Methode zugeordnet werden. Beim Erstellen eines Delegaten wird das an den neuen Ausdruck übergebene Argument ähnlich wie ein Methodenaufruf geschrieben, jedoch ohne die Argumente für die Methode.

public delegate void printStuff(string s); 
... 
printStuff ps1 = new printStuff(WriteToScreen); 
printStuff ps2 = new printStuff(WriteToScreen); 

WriteToScreen und WriteToScreen hier sind eigentlich Methoden mit einem String in params.

Verwandte Themen