指定した名前のパブリック メソッドを検索します。
Overloads Public Function GetMethod( _
ByVal name As String _) As MethodInfo
[C#]
public MethodInfo GetMethod(stringname);
[C++]
public: MethodInfo* GetMethod(String* name);
[JScript]
public function GetMethod(
name : String) : MethodInfo;
パラメータ
- name
取得するパブリック メソッドの名前を格納している String 。
戻り値
指定した名前のパブリック メソッドが存在する場合は、そのパブリック メソッドを表す MethodInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing) 。
例外
例外の種類 | 条件 |
---|---|
AmbiguousMatchException | 指定した名前のメソッドが 2 つ以上存在します。 |
ArgumentNullException | name が null 参照 (Visual Basic では Nothing) です。 |
解説
name の検索では大文字と小文字が区別されます。
要求された型がパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック オブジェクトをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Visual Basic では Nothing) を返します。
メモ コンストラクタおよびメソッドを検索する場合、パラメータは省略できません。パラメータは呼び出すときだけ省略できます。
使用例
[Visual Basic, C#, C++] MyClass で MyFunction のパラメータを取得し、それらの位置とデータ型を表示する例を次に示します。
Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic
Class [MyClass]
Public myInt As Integer = 0
Public myString As String = Nothing
Public Sub New()
End Sub 'New
Public Sub Myfunction(ByVal i As Integer)
End Sub 'Myfunction
End Class '[MyClass]
Class Type_GetMethod
Public Shared Sub Main()
Try
Dim MyObject As New [MyClass]()
Dim myMethodInfo As MethodInfo
' Get the type of MyClass.
Dim myType As Type = MyObject.GetType()
' Get the method information for MyFunction.
myMethodInfo = myType.GetMethod("Myfunction")
' Get the parameters for Myfunction.
Dim myParameters As ParameterInfo() = myMethodInfo.GetParameters()
Console.WriteLine(ControlChars.Cr + "The parameters of the method Myfunction of class MyClass are:" + ControlChars.Cr)
' Display the position and type of the parameters.
Dim i As Integer
For i = 0 To myParameters.Length - 1
Console.WriteLine("The data type of parameter {0} is {1}.", myParameters(i).Position + 1, myParameters(i).ParameterType)
Next i
Catch e As SecurityException
Console.WriteLine(("SecurityException: " + e.Message.ToString()))
Catch e As Exception
Console.WriteLine(("Exception: " + e.Message.ToString()))
End Try
End Sub 'Main
End Class 'Type_GetMethod
[C#]
using System;
using System.Reflection;
using System.Security;
class MyClass
{
public int myInt = 0;
public string myString = null;
public MyClass()
{
}
public void Myfunction(int i)
{
}
}
class Type_GetMethod
{
public static void Main()
{
try
{
MyClass MyObject = new MyClass();
MethodInfo myMethodInfo;
// Get the type of MyClass.
Type myType = MyObject.GetType();
// Get the method information for MyFunction.
myMethodInfo = myType.GetMethod("Myfunction");
// Get the parameters for Myfunction.
ParameterInfo[] myParameters = myMethodInfo.GetParameters();
Console.WriteLine( "\nThe parameters of the method Myfunction of class MyClass are :\n");
// Display the position and type of the parameters.
for(int i = 0; i < myParameters.Length; i++)
Console.WriteLine("The data type of parameter {0} is {1}.",
myParameters[i].Position + 1, myParameters[i].ParameterType);
}
catch (SecurityException e)
{
Console.WriteLine("SecurityException: " + e.Message );
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message );
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
__gc class MyClass {
public:
int* myInt;
String* myString;
MyClass() {
}
void Myfunction(int i) {
myString = __box(i)->ToString();
}
};
int main() {
try {
MyClass* MyObject = new MyClass();
MethodInfo* myMethodInfo;
// Get the type of MyClass.
Type* myType = MyObject->GetType();
// Get the method information for MyFunction.
myMethodInfo = myType->GetMethod(S"Myfunction");
// Get the parameters for Myfunction.
ParameterInfo* myParameters[] = myMethodInfo->GetParameters();
Console::WriteLine(S"\nThe parameters of the method Myfunction of class MyClass are :\n");
// Display the position and type of the parameters.
for (int i = 0; i < myParameters->Length; i++)
Console::WriteLine(S"The data type of parameter {0} is {1}.",
__box(myParameters[i]->Position + 1), myParameters[i]->ParameterType);
} catch (SecurityException* e) {
Console::WriteLine(S"SecurityException: {0}", e->Message);
} catch (Exception* e) {
Console::WriteLine(S"Exception: {0}", e->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
.NET Framework セキュリティ:
- ReflectionPermission (非パブリック オブジェクトをリフレクション操作するために必要なアクセス許可) ReflectionPermissionFlag.TypeInformation (関連する列挙体)
参照
Type クラス | Type メンバ | System 名前空間 | Type.GetMethod オーバーロードの一覧 | MethodInfo | String | DefaultBinder | ReflectionPermission | GetMethodImpl | GetMethods