2011-06-29T23:27:03の更新内容

programming/tips/get_delegate_signature_from_type/index.wiki.txt

current previous
1,101 0,0
+
${smdncms:title,デリゲートの型情報からシグネチャを取得する}
+
${smdncms:keywords,VB.NET,C#,デリゲート,Type,MethodInfo,シグネチャ,型情報}
+
${smdncms:document_versions,codelang=cs,codelang=vb}
+
${smdncms:tags,api/.net,lang/vb,lang/c#}
+

          
+
-関連するページ
+
--[[programming/netfx/delegate]]
+
---[[programming/netfx/delegate/0_abstract]]
+
---[[programming/netfx/delegate/2_operations]]
+

          
+
&msdn(netfx,type,System.Type){Typeクラス};で参照できる型情報には戻り値や引数に関する情報は含まれないため、デリゲート型を表すTypeインスタンスからはデリゲートのシグネチャを直接知ることができない。 そこで、デリゲート型が持つInvokeメソッドの&msdn(netfx,type,System.Reflection.MethodInfo){MethodInfo};を取得し、そこから必要な情報を参照することでデリゲートのシグネチャを知ることができる。
+

          
+
#googleadunit
+

          
+
#tabpage(C#)
+
#code(cs){{
+
using System;
+
using System.Reflection;
+

          
+
class Sample
+
{
+
  public static void Main()
+
  {
+
    PrintDelegateSignature(typeof(EventHandler));
+
    PrintDelegateSignature(typeof(Action<>));
+
    PrintDelegateSignature(typeof(Func<,,,,>));
+
    PrintDelegateSignature(typeof(System.Text.RegularExpressions.MatchEvaluator));
+
  }
+

          
+
  private static void PrintDelegateSignature(Type typeOfDelegate)
+
  {
+
    MethodInfo signature = typeOfDelegate.GetMethod("Invoke");
+

          
+
    Console.Write("{0} {1}(", signature.ReturnType.Name, typeOfDelegate.FullName);
+

          
+
    bool first = true;
+

          
+
    foreach (ParameterInfo p in signature.GetParameters())
+
    {
+
      if (first)
+
        first = false;
+
      else
+
        Console.Write(", ");
+

          
+
      Console.Write("{0} {1}", p.ParameterType.Name, p.Name);
+
    }
+

          
+
    Console.WriteLine(")");
+
  }
+
}
+
}}
+

          
+
#prompt(実行結果){{
+
Void System.EventHandler(Object sender, EventArgs e)
+
Void System.Action`1(T obj)
+
TResult System.Func`5(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
+
String System.Text.RegularExpressions.MatchEvaluator(Match match)
+
}}
+
#tabpage(VB)
+
#code(vb){{
+
Imports System
+
Imports System.Reflection
+

          
+
Class Sample
+
  Public Shared Sub Main()
+
    PrintDelegateSignature(GetType(EventHandler))
+
    PrintDelegateSignature(GetType(Action(Of)))
+
    PrintDelegateSignature(GetType(Func(Of ,,,,)))
+
    PrintDelegateSignature(GetType(System.Text.RegularExpressions.MatchEvaluator))
+
  End Sub
+

          
+
  Private Shared Sub PrintDelegateSignature(ByVal typeOfDelegate As Type)
+
    Dim signature As MethodInfo = typeOfDelegate.GetMethod("Invoke")
+

          
+
    Console.Write("{0}(", typeOfDelegate.FullName)
+

          
+
    Dim first As Boolean = True
+

          
+
    For Each p As ParameterInfo in signature.GetParameters()
+
      If first Then
+
        first = False
+
      Else
+
        Console.Write(", ")
+
      End If
+

          
+
      Console.Write("{1} As {0}", p.ParameterType.Name, p.Name)
+
    Next
+

          
+
    Console.WriteLine(") As {0}", signature.ReturnType.Name)
+
  End Sub
+
End Class
+
}}
+

          
+
#prompt(実行結果){{
+
System.EventHandler(sender As Object, e As EventArgs) As Void
+
System.Action`1(obj As T) As Void
+
System.Func`5(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4) As TResult
+
System.Text.RegularExpressions.MatchEvaluator(match As Match) As String
+
}}
+
#tabpage-end
+