2016-09-03 10 views
0

私はC#で代理人の勉強を始めました。コンパイラは次のコードデリゲートオブジェクトはC#でどのように作成されますか?

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

に遭遇したとき、どのようにデリゲートオブジェクトが作成された私は、すべてのデリゲートが暗黙的にSystem.Delegateクラスを派生していることが分かりました。 「新しいTransformer(Square)」はDelegateクラスのコンストラクタを呼び出して 't'というオブジェクトを作成しますか? .NET FrameworkのためRoslynのリファレンス・ソースを振り返る

答えて

0

System.Delegateクラスのコンストラクタには、次のようになります。4.6.2:

だから、
[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); 
     } 

は、ええ、あなたがこの男から派生型を期待することができます。

あなたは、デリゲートの基礎を探しているなら(私はまだここにそれを維持、質問を読み違えると思う)

構文デリゲートの宣言のための

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

From MSDN:

デリゲートです特定のパラメータリストと戻り値の型を持つメソッドへの参照を表す型です。デリゲートをインスタンス化すると、そのインスタンスを互換性のあるシグネチャと戻り値の型を持つ任意のメソッドに関連付けることができます。デリゲートインスタンスを介してメソッドを呼び出す(または呼び出す)ことができます。

したがって、デリゲート型が宣言されると、デリゲートオブジェクトはnewキーワードで作成され、特定のメソッドに関連付けられなければなりません。デリゲートを作成するとき、新しい式に渡される引数はメソッド呼び出しと同様に記述されますが、メソッドの引数はありません。ここ

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

WriteToScreenWriteToScreenは、実際のparamsの文字列を持つメソッドです。

関連する問題