-
-
Save sonoichi60/e38765d61d3c9a2e0a25ffbdb0735b53 to your computer and use it in GitHub Desktop.
example MethodInfo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 名前指定でメンバ関数の取得(オーバーロードが存在する場合はエラー) | |
var method = typeof(TestClass).GetMethod("Method", BindingFlags.NonPublic | BindingFlags.Instance); | |
// 名前と引数タイプ指定で特定のメンバ関数を取得 | |
var method1 = typeof(TestClass).GetMethod("Method", new Type[] { typeof(int), typeof(int) }); | |
// メンバ関数を全取得 | |
var methods = typeof(TestClass).GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | |
var methodName = method.Name; // 関数名 | |
var returnType = method.ReturnType; // 返り値タイプ | |
var parametors = method.GetParameters(); // 引数情報 | |
var isSpecialName = method.IsSpecialName; // 特殊な名前か | |
foreach (var parametor in parametors) | |
{ | |
var paramType = parametor.ParameterType; // 引数のタイプ | |
if (parametor.HasDefaultValue) // 既定値を持つか | |
{ | |
var defaultValue = parametor.DefaultValue; // 既定値 | |
} | |
} | |
// 関数の実行 | |
var instance = new TestClass(); | |
method1.Invoke(instance, new object[] { 1, 10 }); | |
// 関数の実行(MethodInfoを介さないパターン) | |
typeof(TestClass).InvokeMember("Method", BindingFlags.NonPublic | BindingFlags.Instance, null, instance, new object[] { 2, 2 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment