CSharp examples for System.Reflection:Type
Returns method info of the current type matching the name and the type of parameters
// Copyright (c) Costin Morariu. All rights reserved. using System.Reflection; using System.Collections.Generic; using System;//from w w w . j a va 2 s . co m public class Main{ /// <summary> /// Returns method info of the current type matching the name and the type of parameters /// </summary> /// <param name="type">Current type</param> /// <param name="name">The name of the method</param> /// <param name="parametersTypes">Method parameter types</param> /// <returns>Method info or null in case no match</returns> public static MethodInfo GetMethod(this Type type, string name, Type[] parametersTypes) { var methods = type.GetMethods(name); foreach (var method in methods) { var match = true; var parameters = method.GetParameters(); foreach (var param in parameters) { var valid = true; if (parametersTypes != null) { foreach (var ptype in parametersTypes) { valid &= (ptype == param.ParameterType); } } match &= valid; } if (match) { return method; } } return null; } /// <summary> /// Returns the public methods exposed by the current type matching specified name /// </summary> /// <param name="type">Current type</param> /// <param name="name">The name of the methods</param> /// <returns>Matching method names</returns> public static IEnumerable<MethodInfo> GetMethods(this Type type, string name) { return type.GetTypeInfo().GetDeclaredMethods(name); } }