Object - GetMethod
A T extension method that searches for the public method with the specified name.
public static void Main() { //C# Extension Method: Object - GetMethod MethodInfo StaticMethod = typeof(MethodModel<int>).GetMethod("StaticMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo VirtualMethod = typeof(MethodModel<int>).GetMethod("VirtualMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo ProtectedInternalMethod = typeof(MethodModel<int>).GetMethod("ProtectedInternalMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); Console.WriteLine(StaticMethod.GetDeclaraction()); Console.WriteLine(VirtualMethod.GetDeclaraction()); Console.WriteLine(ProtectedInternalMethod.GetDeclaraction()); } public class MethodModel<T> : AbstractMethodModel { public class Toto<T2> {} public static int StaticMethod() { return 1; } public override int OverrideMethod() { return 1; } public virtual int VirtualMethod() { return 1; } public int PublicMethod() { return 1; } private int PrivateMethod() { return 1; } internal int InternalMethod() { return 1; } protected int ProtectedMethod() { return 1; } protected internal int ProtectedInternalMethod() { return 1; } public int OutParameterMethod(out int i) { i = 1; return 1; } public int RefParameterMethod(ref int i) { return 1; } public int ParamsParameterMethod(params int[] i) { return 1; } public int OptionalParameterMethod(int i = 1) { return 1; } public int InParameterModifierMethod([In] int i) { return 1; } public int OutParameterModifierMethod([Out] int i) { i = 1; return 1; } public int OptionalParameterModifierMethod([Optional] int i) { return 1; } public T GenericTypeMethod(T i) { return i; } public T1 GenericMethod<T1, T2>(T1 i, T2 i2) { return i; } public override int AbstractMethod() { throw new NotImplementedException(); } } public abstract class AbstractMethodModel { public abstract int AbstractMethod(); public abstract int OverrideMethod(); } } public static class ExtensionMethodModel { public static int ExtensionParameterMethod(this int i) { return 1; }
View Source
using System.Reflection; public static partial class Extensions { /// <summary> /// A T extension method that searches for the public method with the specified name. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="name">The string containing the name of the public method to get.</param> /// <returns> /// An object that represents the public method with the specified name, if found; otherwise, null. /// </returns> public static MethodInfo GetMethod<T>(this T @this, string name) { return @this.GetType().GetMethod(name); } /// <summary> /// A T extension method that searches for the specified method whose parameters match the specified argument /// types and modifiers, using the specified binding constraints. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="name">The string containing the name of the public method to get.</param> /// <param name="bindingAttr">A bitmask comprised of one or more BindingFlags that specify how the search is conducted.</param> /// <returns> /// An object that represents the public method with the specified name, if found; otherwise, null. /// </returns> public static MethodInfo GetMethod<T>(this T @this, string name, BindingFlags bindingAttr) { return @this.GetType().GetMethod(name, bindingAttr); } }