System.Type.GetMethod C# (CSharp) Method

GetMethod() public method

Searches for the specified public method whose parameters match the specified argument types.
More than one method is found with the specified name and specified parameters. is null.-or- is null.-or- One of the elements in is null. is multidimensional.
public GetMethod ( string name, Type types ) : MethodInfo
name string The string containing the name of the public method to get.
types Type An array of objects representing the number, order, and type of the parameters for the method to get.-or- An empty array of objects (as provided by the field) to get a method that takes no parameters.
return MethodInfo
        public MethodInfo GetMethod(string name, Type[] types)
        {
            if (name == null)
                throw new ArgumentNullException("name");
            if (types == null)
                throw new ArgumentNullException("types");
            for (int index = 0; index < types.Length; ++index)
            {
                if (types[index] == null)
                    throw new ArgumentNullException("types");
            }
            return GetMethodImpl(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, (Binder)null, CallingConventions.Any, types, (ParameterModifier[])null);
        }

Same methods

Type::GetMethod ( string name ) : MethodInfo
Type::GetMethod ( string name, BindingFlags bindingAttr ) : MethodInfo

Usage Example

Example #1
0
        public static bool GetPrimitives(Type containerType, Type type, out MethodInfo writer, out MethodInfo reader)
        {
            if (type.IsEnum)
                type = Enum.GetUnderlyingType(type);

            if (type.IsGenericType == false)
            {
                writer = containerType.GetMethod("WritePrimitive", BindingFlags.Static | BindingFlags.Public | BindingFlags.ExactBinding, null,
                    new Type[] { typeof(Stream), type }, null);

                reader = containerType.GetMethod("ReadPrimitive", BindingFlags.Static | BindingFlags.Public | BindingFlags.ExactBinding, null,
                    new Type[] { typeof(Stream), type.MakeByRefType() }, null);
            }
            else
            {
                var genType = type.GetGenericTypeDefinition();

                writer = GetGenWriter(containerType, genType);
                reader = GetGenReader(containerType, genType);
            }

            if (writer == null && reader == null)
                return false;
            else if (writer != null && reader != null)
                return true;
            else
                throw new InvalidOperationException(String.Format("Missing a {0}Primitive() for {1}",
                    reader == null ? "Read" : "Write", type.FullName));
        }
All Usage Examples Of System.Type::GetMethod